zstd_lazy.c 97 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104
  1. /*
  2. * Copyright (c) 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 : curr >= btlow == (curr - btmask)
  50. * doesn't fail */
  51. static void
  52. ZSTD_insertDUBT1(const ZSTD_matchState_t* ms,
  53. U32 curr, 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 = (curr>=dictLimit) ? base + curr : dictBase + curr;
  66. const BYTE* const iend = (curr>=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*(curr&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 windowValid = ms->window.lowLimit;
  75. U32 const maxDistance = 1U << cParams->windowLog;
  76. U32 const windowLow = (curr - windowValid > maxDistance) ? curr - maxDistance : windowValid;
  77. DEBUGLOG(8, "ZSTD_insertDUBT1(%u) (dictLimit=%u, lowLimit=%u)",
  78. curr, dictLimit, windowLow);
  79. assert(curr >= btLow);
  80. assert(ip < iend); /* condition for ZSTD_count */
  81. for (; nbCompares && (matchIndex > windowLow); --nbCompares) {
  82. U32* const nextPtr = bt + 2*(matchIndex & btMask);
  83. size_t matchLength = MIN(commonLengthSmaller, commonLengthLarger); /* guaranteed minimum nb of common bytes */
  84. assert(matchIndex < curr);
  85. /* note : all candidates are now supposed sorted,
  86. * but it's still possible to have nextPtr[1] == ZSTD_DUBT_UNSORTED_MARK
  87. * when a real index has the same value as ZSTD_DUBT_UNSORTED_MARK */
  88. if ( (dictMode != ZSTD_extDict)
  89. || (matchIndex+matchLength >= dictLimit) /* both in current segment*/
  90. || (curr < dictLimit) /* both in extDict */) {
  91. const BYTE* const mBase = ( (dictMode != ZSTD_extDict)
  92. || (matchIndex+matchLength >= dictLimit)) ?
  93. base : dictBase;
  94. assert( (matchIndex+matchLength >= dictLimit) /* might be wrong if extDict is incorrectly set to 0 */
  95. || (curr < dictLimit) );
  96. match = mBase + matchIndex;
  97. matchLength += ZSTD_count(ip+matchLength, match+matchLength, iend);
  98. } else {
  99. match = dictBase + matchIndex;
  100. matchLength += ZSTD_count_2segments(ip+matchLength, match+matchLength, iend, dictEnd, prefixStart);
  101. if (matchIndex+matchLength >= dictLimit)
  102. match = base + matchIndex; /* preparation for next read of match[matchLength] */
  103. }
  104. DEBUGLOG(8, "ZSTD_insertDUBT1: comparing %u with %u : found %u common bytes ",
  105. curr, matchIndex, (U32)matchLength);
  106. if (ip+matchLength == iend) { /* equal : no way to know if inf or sup */
  107. break; /* drop , to guarantee consistency ; miss a bit of compression, but other solutions can corrupt tree */
  108. }
  109. if (match[matchLength] < ip[matchLength]) { /* necessarily within buffer */
  110. /* match is smaller than current */
  111. *smallerPtr = matchIndex; /* update smaller idx */
  112. commonLengthSmaller = matchLength; /* all smaller will now have at least this guaranteed common length */
  113. if (matchIndex <= btLow) { smallerPtr=&dummy32; break; } /* beyond tree size, stop searching */
  114. DEBUGLOG(8, "ZSTD_insertDUBT1: %u (>btLow=%u) is smaller : next => %u",
  115. matchIndex, btLow, nextPtr[1]);
  116. smallerPtr = nextPtr+1; /* new "candidate" => larger than match, which was smaller than target */
  117. matchIndex = nextPtr[1]; /* new matchIndex, larger than previous and closer to current */
  118. } else {
  119. /* match is larger than current */
  120. *largerPtr = matchIndex;
  121. commonLengthLarger = matchLength;
  122. if (matchIndex <= btLow) { largerPtr=&dummy32; break; } /* beyond tree size, stop searching */
  123. DEBUGLOG(8, "ZSTD_insertDUBT1: %u (>btLow=%u) is larger => %u",
  124. matchIndex, btLow, nextPtr[0]);
  125. largerPtr = nextPtr;
  126. matchIndex = nextPtr[0];
  127. } }
  128. *smallerPtr = *largerPtr = 0;
  129. }
  130. static size_t
  131. ZSTD_DUBT_findBetterDictMatch (
  132. const ZSTD_matchState_t* ms,
  133. const BYTE* const ip, const BYTE* const iend,
  134. size_t* offsetPtr,
  135. size_t bestLength,
  136. U32 nbCompares,
  137. U32 const mls,
  138. const ZSTD_dictMode_e dictMode)
  139. {
  140. const ZSTD_matchState_t * const dms = ms->dictMatchState;
  141. const ZSTD_compressionParameters* const dmsCParams = &dms->cParams;
  142. const U32 * const dictHashTable = dms->hashTable;
  143. U32 const hashLog = dmsCParams->hashLog;
  144. size_t const h = ZSTD_hashPtr(ip, hashLog, mls);
  145. U32 dictMatchIndex = dictHashTable[h];
  146. const BYTE* const base = ms->window.base;
  147. const BYTE* const prefixStart = base + ms->window.dictLimit;
  148. U32 const curr = (U32)(ip-base);
  149. const BYTE* const dictBase = dms->window.base;
  150. const BYTE* const dictEnd = dms->window.nextSrc;
  151. U32 const dictHighLimit = (U32)(dms->window.nextSrc - dms->window.base);
  152. U32 const dictLowLimit = dms->window.lowLimit;
  153. U32 const dictIndexDelta = ms->window.lowLimit - dictHighLimit;
  154. U32* const dictBt = dms->chainTable;
  155. U32 const btLog = dmsCParams->chainLog - 1;
  156. U32 const btMask = (1 << btLog) - 1;
  157. U32 const btLow = (btMask >= dictHighLimit - dictLowLimit) ? dictLowLimit : dictHighLimit - btMask;
  158. size_t commonLengthSmaller=0, commonLengthLarger=0;
  159. (void)dictMode;
  160. assert(dictMode == ZSTD_dictMatchState);
  161. for (; nbCompares && (dictMatchIndex > dictLowLimit); --nbCompares) {
  162. U32* const nextPtr = dictBt + 2*(dictMatchIndex & btMask);
  163. size_t matchLength = MIN(commonLengthSmaller, commonLengthLarger); /* guaranteed minimum nb of common bytes */
  164. const BYTE* match = dictBase + dictMatchIndex;
  165. matchLength += ZSTD_count_2segments(ip+matchLength, match+matchLength, iend, dictEnd, prefixStart);
  166. if (dictMatchIndex+matchLength >= dictHighLimit)
  167. match = base + dictMatchIndex + dictIndexDelta; /* to prepare for next usage of match[matchLength] */
  168. if (matchLength > bestLength) {
  169. U32 matchIndex = dictMatchIndex + dictIndexDelta;
  170. if ( (4*(int)(matchLength-bestLength)) > (int)(ZSTD_highbit32(curr-matchIndex+1) - ZSTD_highbit32((U32)offsetPtr[0]+1)) ) {
  171. DEBUGLOG(9, "ZSTD_DUBT_findBetterDictMatch(%u) : found better match length %u -> %u and offsetCode %u -> %u (dictMatchIndex %u, matchIndex %u)",
  172. curr, (U32)bestLength, (U32)matchLength, (U32)*offsetPtr, STORE_OFFSET(curr - matchIndex), dictMatchIndex, matchIndex);
  173. bestLength = matchLength, *offsetPtr = STORE_OFFSET(curr - matchIndex);
  174. }
  175. 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 */
  176. break; /* drop, to guarantee consistency (miss a little bit of compression) */
  177. }
  178. }
  179. if (match[matchLength] < ip[matchLength]) {
  180. if (dictMatchIndex <= btLow) { break; } /* beyond tree size, stop the search */
  181. commonLengthSmaller = matchLength; /* all smaller will now have at least this guaranteed common length */
  182. dictMatchIndex = nextPtr[1]; /* new matchIndex larger than previous (closer to current) */
  183. } else {
  184. /* match is larger than current */
  185. if (dictMatchIndex <= btLow) { break; } /* beyond tree size, stop the search */
  186. commonLengthLarger = matchLength;
  187. dictMatchIndex = nextPtr[0];
  188. }
  189. }
  190. if (bestLength >= MINMATCH) {
  191. U32 const mIndex = curr - (U32)STORED_OFFSET(*offsetPtr); (void)mIndex;
  192. DEBUGLOG(8, "ZSTD_DUBT_findBetterDictMatch(%u) : found match of length %u and offsetCode %u (pos %u)",
  193. curr, (U32)bestLength, (U32)*offsetPtr, mIndex);
  194. }
  195. return bestLength;
  196. }
  197. static size_t
  198. ZSTD_DUBT_findBestMatch(ZSTD_matchState_t* ms,
  199. const BYTE* const ip, const BYTE* const iend,
  200. size_t* offsetPtr,
  201. U32 const mls,
  202. const ZSTD_dictMode_e dictMode)
  203. {
  204. const ZSTD_compressionParameters* const cParams = &ms->cParams;
  205. U32* const hashTable = ms->hashTable;
  206. U32 const hashLog = cParams->hashLog;
  207. size_t const h = ZSTD_hashPtr(ip, hashLog, mls);
  208. U32 matchIndex = hashTable[h];
  209. const BYTE* const base = ms->window.base;
  210. U32 const curr = (U32)(ip-base);
  211. U32 const windowLow = ZSTD_getLowestMatchIndex(ms, curr, cParams->windowLog);
  212. U32* const bt = ms->chainTable;
  213. U32 const btLog = cParams->chainLog - 1;
  214. U32 const btMask = (1 << btLog) - 1;
  215. U32 const btLow = (btMask >= curr) ? 0 : curr - btMask;
  216. U32 const unsortLimit = MAX(btLow, windowLow);
  217. U32* nextCandidate = bt + 2*(matchIndex&btMask);
  218. U32* unsortedMark = bt + 2*(matchIndex&btMask) + 1;
  219. U32 nbCompares = 1U << cParams->searchLog;
  220. U32 nbCandidates = nbCompares;
  221. U32 previousCandidate = 0;
  222. DEBUGLOG(7, "ZSTD_DUBT_findBestMatch (%u) ", curr);
  223. assert(ip <= iend-8); /* required for h calculation */
  224. assert(dictMode != ZSTD_dedicatedDictSearch);
  225. /* reach end of unsorted candidates list */
  226. while ( (matchIndex > unsortLimit)
  227. && (*unsortedMark == ZSTD_DUBT_UNSORTED_MARK)
  228. && (nbCandidates > 1) ) {
  229. DEBUGLOG(8, "ZSTD_DUBT_findBestMatch: candidate %u is unsorted",
  230. matchIndex);
  231. *unsortedMark = previousCandidate; /* the unsortedMark becomes a reversed chain, to move up back to original position */
  232. previousCandidate = matchIndex;
  233. matchIndex = *nextCandidate;
  234. nextCandidate = bt + 2*(matchIndex&btMask);
  235. unsortedMark = bt + 2*(matchIndex&btMask) + 1;
  236. nbCandidates --;
  237. }
  238. /* nullify last candidate if it's still unsorted
  239. * simplification, detrimental to compression ratio, beneficial for speed */
  240. if ( (matchIndex > unsortLimit)
  241. && (*unsortedMark==ZSTD_DUBT_UNSORTED_MARK) ) {
  242. DEBUGLOG(7, "ZSTD_DUBT_findBestMatch: nullify last unsorted candidate %u",
  243. matchIndex);
  244. *nextCandidate = *unsortedMark = 0;
  245. }
  246. /* batch sort stacked candidates */
  247. matchIndex = previousCandidate;
  248. while (matchIndex) { /* will end on matchIndex == 0 */
  249. U32* const nextCandidateIdxPtr = bt + 2*(matchIndex&btMask) + 1;
  250. U32 const nextCandidateIdx = *nextCandidateIdxPtr;
  251. ZSTD_insertDUBT1(ms, matchIndex, iend,
  252. nbCandidates, unsortLimit, dictMode);
  253. matchIndex = nextCandidateIdx;
  254. nbCandidates++;
  255. }
  256. /* find longest match */
  257. { size_t commonLengthSmaller = 0, commonLengthLarger = 0;
  258. const BYTE* const dictBase = ms->window.dictBase;
  259. const U32 dictLimit = ms->window.dictLimit;
  260. const BYTE* const dictEnd = dictBase + dictLimit;
  261. const BYTE* const prefixStart = base + dictLimit;
  262. U32* smallerPtr = bt + 2*(curr&btMask);
  263. U32* largerPtr = bt + 2*(curr&btMask) + 1;
  264. U32 matchEndIdx = curr + 8 + 1;
  265. U32 dummy32; /* to be nullified at the end */
  266. size_t bestLength = 0;
  267. matchIndex = hashTable[h];
  268. hashTable[h] = curr; /* Update Hash Table */
  269. for (; nbCompares && (matchIndex > windowLow); --nbCompares) {
  270. U32* const nextPtr = bt + 2*(matchIndex & btMask);
  271. size_t matchLength = MIN(commonLengthSmaller, commonLengthLarger); /* guaranteed minimum nb of common bytes */
  272. const BYTE* match;
  273. if ((dictMode != ZSTD_extDict) || (matchIndex+matchLength >= dictLimit)) {
  274. match = base + matchIndex;
  275. matchLength += ZSTD_count(ip+matchLength, match+matchLength, iend);
  276. } else {
  277. match = dictBase + matchIndex;
  278. matchLength += ZSTD_count_2segments(ip+matchLength, match+matchLength, iend, dictEnd, prefixStart);
  279. if (matchIndex+matchLength >= dictLimit)
  280. match = base + matchIndex; /* to prepare for next usage of match[matchLength] */
  281. }
  282. if (matchLength > bestLength) {
  283. if (matchLength > matchEndIdx - matchIndex)
  284. matchEndIdx = matchIndex + (U32)matchLength;
  285. if ( (4*(int)(matchLength-bestLength)) > (int)(ZSTD_highbit32(curr-matchIndex+1) - ZSTD_highbit32((U32)offsetPtr[0]+1)) )
  286. bestLength = matchLength, *offsetPtr = STORE_OFFSET(curr - matchIndex);
  287. if (ip+matchLength == iend) { /* equal : no way to know if inf or sup */
  288. if (dictMode == ZSTD_dictMatchState) {
  289. nbCompares = 0; /* in addition to avoiding checking any
  290. * further in this loop, make sure we
  291. * skip checking in the dictionary. */
  292. }
  293. break; /* drop, to guarantee consistency (miss a little bit of compression) */
  294. }
  295. }
  296. if (match[matchLength] < ip[matchLength]) {
  297. /* match is smaller than current */
  298. *smallerPtr = matchIndex; /* update smaller idx */
  299. commonLengthSmaller = matchLength; /* all smaller will now have at least this guaranteed common length */
  300. if (matchIndex <= btLow) { smallerPtr=&dummy32; break; } /* beyond tree size, stop the search */
  301. smallerPtr = nextPtr+1; /* new "smaller" => larger of match */
  302. matchIndex = nextPtr[1]; /* new matchIndex larger than previous (closer to current) */
  303. } else {
  304. /* match is larger than current */
  305. *largerPtr = matchIndex;
  306. commonLengthLarger = matchLength;
  307. if (matchIndex <= btLow) { largerPtr=&dummy32; break; } /* beyond tree size, stop the search */
  308. largerPtr = nextPtr;
  309. matchIndex = nextPtr[0];
  310. } }
  311. *smallerPtr = *largerPtr = 0;
  312. assert(nbCompares <= (1U << ZSTD_SEARCHLOG_MAX)); /* Check we haven't underflowed. */
  313. if (dictMode == ZSTD_dictMatchState && nbCompares) {
  314. bestLength = ZSTD_DUBT_findBetterDictMatch(
  315. ms, ip, iend,
  316. offsetPtr, bestLength, nbCompares,
  317. mls, dictMode);
  318. }
  319. assert(matchEndIdx > curr+8); /* ensure nextToUpdate is increased */
  320. ms->nextToUpdate = matchEndIdx - 8; /* skip repetitive patterns */
  321. if (bestLength >= MINMATCH) {
  322. U32 const mIndex = curr - (U32)STORED_OFFSET(*offsetPtr); (void)mIndex;
  323. DEBUGLOG(8, "ZSTD_DUBT_findBestMatch(%u) : found match of length %u and offsetCode %u (pos %u)",
  324. curr, (U32)bestLength, (U32)*offsetPtr, mIndex);
  325. }
  326. return bestLength;
  327. }
  328. }
  329. /** ZSTD_BtFindBestMatch() : Tree updater, providing best match */
  330. FORCE_INLINE_TEMPLATE size_t
  331. ZSTD_BtFindBestMatch( ZSTD_matchState_t* ms,
  332. const BYTE* const ip, const BYTE* const iLimit,
  333. size_t* offsetPtr,
  334. const U32 mls /* template */,
  335. const ZSTD_dictMode_e dictMode)
  336. {
  337. DEBUGLOG(7, "ZSTD_BtFindBestMatch");
  338. if (ip < ms->window.base + ms->nextToUpdate) return 0; /* skipped area */
  339. ZSTD_updateDUBT(ms, ip, iLimit, mls);
  340. return ZSTD_DUBT_findBestMatch(ms, ip, iLimit, offsetPtr, mls, dictMode);
  341. }
  342. /***********************************
  343. * Dedicated dict search
  344. ***********************************/
  345. void ZSTD_dedicatedDictSearch_lazy_loadDictionary(ZSTD_matchState_t* ms, const BYTE* const ip)
  346. {
  347. const BYTE* const base = ms->window.base;
  348. U32 const target = (U32)(ip - base);
  349. U32* const hashTable = ms->hashTable;
  350. U32* const chainTable = ms->chainTable;
  351. U32 const chainSize = 1 << ms->cParams.chainLog;
  352. U32 idx = ms->nextToUpdate;
  353. U32 const minChain = chainSize < target - idx ? target - chainSize : idx;
  354. U32 const bucketSize = 1 << ZSTD_LAZY_DDSS_BUCKET_LOG;
  355. U32 const cacheSize = bucketSize - 1;
  356. U32 const chainAttempts = (1 << ms->cParams.searchLog) - cacheSize;
  357. U32 const chainLimit = chainAttempts > 255 ? 255 : chainAttempts;
  358. /* We know the hashtable is oversized by a factor of `bucketSize`.
  359. * We are going to temporarily pretend `bucketSize == 1`, keeping only a
  360. * single entry. We will use the rest of the space to construct a temporary
  361. * chaintable.
  362. */
  363. U32 const hashLog = ms->cParams.hashLog - ZSTD_LAZY_DDSS_BUCKET_LOG;
  364. U32* const tmpHashTable = hashTable;
  365. U32* const tmpChainTable = hashTable + ((size_t)1 << hashLog);
  366. U32 const tmpChainSize = (U32)((1 << ZSTD_LAZY_DDSS_BUCKET_LOG) - 1) << hashLog;
  367. U32 const tmpMinChain = tmpChainSize < target ? target - tmpChainSize : idx;
  368. U32 hashIdx;
  369. assert(ms->cParams.chainLog <= 24);
  370. assert(ms->cParams.hashLog > ms->cParams.chainLog);
  371. assert(idx != 0);
  372. assert(tmpMinChain <= minChain);
  373. /* fill conventional hash table and conventional chain table */
  374. for ( ; idx < target; idx++) {
  375. U32 const h = (U32)ZSTD_hashPtr(base + idx, hashLog, ms->cParams.minMatch);
  376. if (idx >= tmpMinChain) {
  377. tmpChainTable[idx - tmpMinChain] = hashTable[h];
  378. }
  379. tmpHashTable[h] = idx;
  380. }
  381. /* sort chains into ddss chain table */
  382. {
  383. U32 chainPos = 0;
  384. for (hashIdx = 0; hashIdx < (1U << hashLog); hashIdx++) {
  385. U32 count;
  386. U32 countBeyondMinChain = 0;
  387. U32 i = tmpHashTable[hashIdx];
  388. for (count = 0; i >= tmpMinChain && count < cacheSize; count++) {
  389. /* skip through the chain to the first position that won't be
  390. * in the hash cache bucket */
  391. if (i < minChain) {
  392. countBeyondMinChain++;
  393. }
  394. i = tmpChainTable[i - tmpMinChain];
  395. }
  396. if (count == cacheSize) {
  397. for (count = 0; count < chainLimit;) {
  398. if (i < minChain) {
  399. if (!i || ++countBeyondMinChain > cacheSize) {
  400. /* only allow pulling `cacheSize` number of entries
  401. * into the cache or chainTable beyond `minChain`,
  402. * to replace the entries pulled out of the
  403. * chainTable into the cache. This lets us reach
  404. * back further without increasing the total number
  405. * of entries in the chainTable, guaranteeing the
  406. * DDSS chain table will fit into the space
  407. * allocated for the regular one. */
  408. break;
  409. }
  410. }
  411. chainTable[chainPos++] = i;
  412. count++;
  413. if (i < tmpMinChain) {
  414. break;
  415. }
  416. i = tmpChainTable[i - tmpMinChain];
  417. }
  418. } else {
  419. count = 0;
  420. }
  421. if (count) {
  422. tmpHashTable[hashIdx] = ((chainPos - count) << 8) + count;
  423. } else {
  424. tmpHashTable[hashIdx] = 0;
  425. }
  426. }
  427. assert(chainPos <= chainSize); /* I believe this is guaranteed... */
  428. }
  429. /* move chain pointers into the last entry of each hash bucket */
  430. for (hashIdx = (1 << hashLog); hashIdx; ) {
  431. U32 const bucketIdx = --hashIdx << ZSTD_LAZY_DDSS_BUCKET_LOG;
  432. U32 const chainPackedPointer = tmpHashTable[hashIdx];
  433. U32 i;
  434. for (i = 0; i < cacheSize; i++) {
  435. hashTable[bucketIdx + i] = 0;
  436. }
  437. hashTable[bucketIdx + bucketSize - 1] = chainPackedPointer;
  438. }
  439. /* fill the buckets of the hash table */
  440. for (idx = ms->nextToUpdate; idx < target; idx++) {
  441. U32 const h = (U32)ZSTD_hashPtr(base + idx, hashLog, ms->cParams.minMatch)
  442. << ZSTD_LAZY_DDSS_BUCKET_LOG;
  443. U32 i;
  444. /* Shift hash cache down 1. */
  445. for (i = cacheSize - 1; i; i--)
  446. hashTable[h + i] = hashTable[h + i - 1];
  447. hashTable[h] = idx;
  448. }
  449. ms->nextToUpdate = target;
  450. }
  451. /* Returns the longest match length found in the dedicated dict search structure.
  452. * If none are longer than the argument ml, then ml will be returned.
  453. */
  454. FORCE_INLINE_TEMPLATE
  455. size_t ZSTD_dedicatedDictSearch_lazy_search(size_t* offsetPtr, size_t ml, U32 nbAttempts,
  456. const ZSTD_matchState_t* const dms,
  457. const BYTE* const ip, const BYTE* const iLimit,
  458. const BYTE* const prefixStart, const U32 curr,
  459. const U32 dictLimit, const size_t ddsIdx) {
  460. const U32 ddsLowestIndex = dms->window.dictLimit;
  461. const BYTE* const ddsBase = dms->window.base;
  462. const BYTE* const ddsEnd = dms->window.nextSrc;
  463. const U32 ddsSize = (U32)(ddsEnd - ddsBase);
  464. const U32 ddsIndexDelta = dictLimit - ddsSize;
  465. const U32 bucketSize = (1 << ZSTD_LAZY_DDSS_BUCKET_LOG);
  466. const U32 bucketLimit = nbAttempts < bucketSize - 1 ? nbAttempts : bucketSize - 1;
  467. U32 ddsAttempt;
  468. U32 matchIndex;
  469. for (ddsAttempt = 0; ddsAttempt < bucketSize - 1; ddsAttempt++) {
  470. PREFETCH_L1(ddsBase + dms->hashTable[ddsIdx + ddsAttempt]);
  471. }
  472. {
  473. U32 const chainPackedPointer = dms->hashTable[ddsIdx + bucketSize - 1];
  474. U32 const chainIndex = chainPackedPointer >> 8;
  475. PREFETCH_L1(&dms->chainTable[chainIndex]);
  476. }
  477. for (ddsAttempt = 0; ddsAttempt < bucketLimit; ddsAttempt++) {
  478. size_t currentMl=0;
  479. const BYTE* match;
  480. matchIndex = dms->hashTable[ddsIdx + ddsAttempt];
  481. match = ddsBase + matchIndex;
  482. if (!matchIndex) {
  483. return ml;
  484. }
  485. /* guaranteed by table construction */
  486. (void)ddsLowestIndex;
  487. assert(matchIndex >= ddsLowestIndex);
  488. assert(match+4 <= ddsEnd);
  489. if (MEM_read32(match) == MEM_read32(ip)) {
  490. /* assumption : matchIndex <= dictLimit-4 (by table construction) */
  491. currentMl = ZSTD_count_2segments(ip+4, match+4, iLimit, ddsEnd, prefixStart) + 4;
  492. }
  493. /* save best solution */
  494. if (currentMl > ml) {
  495. ml = currentMl;
  496. *offsetPtr = STORE_OFFSET(curr - (matchIndex + ddsIndexDelta));
  497. if (ip+currentMl == iLimit) {
  498. /* best possible, avoids read overflow on next attempt */
  499. return ml;
  500. }
  501. }
  502. }
  503. {
  504. U32 const chainPackedPointer = dms->hashTable[ddsIdx + bucketSize - 1];
  505. U32 chainIndex = chainPackedPointer >> 8;
  506. U32 const chainLength = chainPackedPointer & 0xFF;
  507. U32 const chainAttempts = nbAttempts - ddsAttempt;
  508. U32 const chainLimit = chainAttempts > chainLength ? chainLength : chainAttempts;
  509. U32 chainAttempt;
  510. for (chainAttempt = 0 ; chainAttempt < chainLimit; chainAttempt++) {
  511. PREFETCH_L1(ddsBase + dms->chainTable[chainIndex + chainAttempt]);
  512. }
  513. for (chainAttempt = 0 ; chainAttempt < chainLimit; chainAttempt++, chainIndex++) {
  514. size_t currentMl=0;
  515. const BYTE* match;
  516. matchIndex = dms->chainTable[chainIndex];
  517. match = ddsBase + matchIndex;
  518. /* guaranteed by table construction */
  519. assert(matchIndex >= ddsLowestIndex);
  520. assert(match+4 <= ddsEnd);
  521. if (MEM_read32(match) == MEM_read32(ip)) {
  522. /* assumption : matchIndex <= dictLimit-4 (by table construction) */
  523. currentMl = ZSTD_count_2segments(ip+4, match+4, iLimit, ddsEnd, prefixStart) + 4;
  524. }
  525. /* save best solution */
  526. if (currentMl > ml) {
  527. ml = currentMl;
  528. *offsetPtr = STORE_OFFSET(curr - (matchIndex + ddsIndexDelta));
  529. if (ip+currentMl == iLimit) break; /* best possible, avoids read overflow on next attempt */
  530. }
  531. }
  532. }
  533. return ml;
  534. }
  535. /* *********************************
  536. * Hash Chain
  537. ***********************************/
  538. #define NEXT_IN_CHAIN(d, mask) chainTable[(d) & (mask)]
  539. /* Update chains up to ip (excluded)
  540. Assumption : always within prefix (i.e. not within extDict) */
  541. FORCE_INLINE_TEMPLATE U32 ZSTD_insertAndFindFirstIndex_internal(
  542. ZSTD_matchState_t* ms,
  543. const ZSTD_compressionParameters* const cParams,
  544. const BYTE* ip, U32 const mls)
  545. {
  546. U32* const hashTable = ms->hashTable;
  547. const U32 hashLog = cParams->hashLog;
  548. U32* const chainTable = ms->chainTable;
  549. const U32 chainMask = (1 << cParams->chainLog) - 1;
  550. const BYTE* const base = ms->window.base;
  551. const U32 target = (U32)(ip - base);
  552. U32 idx = ms->nextToUpdate;
  553. while(idx < target) { /* catch up */
  554. size_t const h = ZSTD_hashPtr(base+idx, hashLog, mls);
  555. NEXT_IN_CHAIN(idx, chainMask) = hashTable[h];
  556. hashTable[h] = idx;
  557. idx++;
  558. }
  559. ms->nextToUpdate = target;
  560. return hashTable[ZSTD_hashPtr(ip, hashLog, mls)];
  561. }
  562. U32 ZSTD_insertAndFindFirstIndex(ZSTD_matchState_t* ms, const BYTE* ip) {
  563. const ZSTD_compressionParameters* const cParams = &ms->cParams;
  564. return ZSTD_insertAndFindFirstIndex_internal(ms, cParams, ip, ms->cParams.minMatch);
  565. }
  566. /* inlining is important to hardwire a hot branch (template emulation) */
  567. FORCE_INLINE_TEMPLATE
  568. size_t ZSTD_HcFindBestMatch(
  569. ZSTD_matchState_t* ms,
  570. const BYTE* const ip, const BYTE* const iLimit,
  571. size_t* offsetPtr,
  572. const U32 mls, const ZSTD_dictMode_e dictMode)
  573. {
  574. const ZSTD_compressionParameters* const cParams = &ms->cParams;
  575. U32* const chainTable = ms->chainTable;
  576. const U32 chainSize = (1 << cParams->chainLog);
  577. const U32 chainMask = chainSize-1;
  578. const BYTE* const base = ms->window.base;
  579. const BYTE* const dictBase = ms->window.dictBase;
  580. const U32 dictLimit = ms->window.dictLimit;
  581. const BYTE* const prefixStart = base + dictLimit;
  582. const BYTE* const dictEnd = dictBase + dictLimit;
  583. const U32 curr = (U32)(ip-base);
  584. const U32 maxDistance = 1U << cParams->windowLog;
  585. const U32 lowestValid = ms->window.lowLimit;
  586. const U32 withinMaxDistance = (curr - lowestValid > maxDistance) ? curr - maxDistance : lowestValid;
  587. const U32 isDictionary = (ms->loadedDictEnd != 0);
  588. const U32 lowLimit = isDictionary ? lowestValid : withinMaxDistance;
  589. const U32 minChain = curr > chainSize ? curr - chainSize : 0;
  590. U32 nbAttempts = 1U << cParams->searchLog;
  591. size_t ml=4-1;
  592. const ZSTD_matchState_t* const dms = ms->dictMatchState;
  593. const U32 ddsHashLog = dictMode == ZSTD_dedicatedDictSearch
  594. ? dms->cParams.hashLog - ZSTD_LAZY_DDSS_BUCKET_LOG : 0;
  595. const size_t ddsIdx = dictMode == ZSTD_dedicatedDictSearch
  596. ? ZSTD_hashPtr(ip, ddsHashLog, mls) << ZSTD_LAZY_DDSS_BUCKET_LOG : 0;
  597. U32 matchIndex;
  598. if (dictMode == ZSTD_dedicatedDictSearch) {
  599. const U32* entry = &dms->hashTable[ddsIdx];
  600. PREFETCH_L1(entry);
  601. }
  602. /* HC4 match finder */
  603. matchIndex = ZSTD_insertAndFindFirstIndex_internal(ms, cParams, ip, mls);
  604. for ( ; (matchIndex>=lowLimit) & (nbAttempts>0) ; nbAttempts--) {
  605. size_t currentMl=0;
  606. if ((dictMode != ZSTD_extDict) || matchIndex >= dictLimit) {
  607. const BYTE* const match = base + matchIndex;
  608. assert(matchIndex >= dictLimit); /* ensures this is true if dictMode != ZSTD_extDict */
  609. if (match[ml] == ip[ml]) /* potentially better */
  610. currentMl = ZSTD_count(ip, match, iLimit);
  611. } else {
  612. const BYTE* const match = dictBase + matchIndex;
  613. assert(match+4 <= dictEnd);
  614. if (MEM_read32(match) == MEM_read32(ip)) /* assumption : matchIndex <= dictLimit-4 (by table construction) */
  615. currentMl = ZSTD_count_2segments(ip+4, match+4, iLimit, dictEnd, prefixStart) + 4;
  616. }
  617. /* save best solution */
  618. if (currentMl > ml) {
  619. ml = currentMl;
  620. *offsetPtr = STORE_OFFSET(curr - matchIndex);
  621. if (ip+currentMl == iLimit) break; /* best possible, avoids read overflow on next attempt */
  622. }
  623. if (matchIndex <= minChain) break;
  624. matchIndex = NEXT_IN_CHAIN(matchIndex, chainMask);
  625. }
  626. assert(nbAttempts <= (1U << ZSTD_SEARCHLOG_MAX)); /* Check we haven't underflowed. */
  627. if (dictMode == ZSTD_dedicatedDictSearch) {
  628. ml = ZSTD_dedicatedDictSearch_lazy_search(offsetPtr, ml, nbAttempts, dms,
  629. ip, iLimit, prefixStart, curr, dictLimit, ddsIdx);
  630. } else if (dictMode == ZSTD_dictMatchState) {
  631. const U32* const dmsChainTable = dms->chainTable;
  632. const U32 dmsChainSize = (1 << dms->cParams.chainLog);
  633. const U32 dmsChainMask = dmsChainSize - 1;
  634. const U32 dmsLowestIndex = dms->window.dictLimit;
  635. const BYTE* const dmsBase = dms->window.base;
  636. const BYTE* const dmsEnd = dms->window.nextSrc;
  637. const U32 dmsSize = (U32)(dmsEnd - dmsBase);
  638. const U32 dmsIndexDelta = dictLimit - dmsSize;
  639. const U32 dmsMinChain = dmsSize > dmsChainSize ? dmsSize - dmsChainSize : 0;
  640. matchIndex = dms->hashTable[ZSTD_hashPtr(ip, dms->cParams.hashLog, mls)];
  641. for ( ; (matchIndex>=dmsLowestIndex) & (nbAttempts>0) ; nbAttempts--) {
  642. size_t currentMl=0;
  643. const BYTE* const match = dmsBase + matchIndex;
  644. assert(match+4 <= dmsEnd);
  645. if (MEM_read32(match) == MEM_read32(ip)) /* assumption : matchIndex <= dictLimit-4 (by table construction) */
  646. currentMl = ZSTD_count_2segments(ip+4, match+4, iLimit, dmsEnd, prefixStart) + 4;
  647. /* save best solution */
  648. if (currentMl > ml) {
  649. ml = currentMl;
  650. assert(curr > matchIndex + dmsIndexDelta);
  651. *offsetPtr = STORE_OFFSET(curr - (matchIndex + dmsIndexDelta));
  652. if (ip+currentMl == iLimit) break; /* best possible, avoids read overflow on next attempt */
  653. }
  654. if (matchIndex <= dmsMinChain) break;
  655. matchIndex = dmsChainTable[matchIndex & dmsChainMask];
  656. }
  657. }
  658. return ml;
  659. }
  660. /* *********************************
  661. * (SIMD) Row-based matchfinder
  662. ***********************************/
  663. /* Constants for row-based hash */
  664. #define ZSTD_ROW_HASH_TAG_OFFSET 16 /* byte offset of hashes in the match state's tagTable from the beginning of a row */
  665. #define ZSTD_ROW_HASH_TAG_BITS 8 /* nb bits to use for the tag */
  666. #define ZSTD_ROW_HASH_TAG_MASK ((1u << ZSTD_ROW_HASH_TAG_BITS) - 1)
  667. #define ZSTD_ROW_HASH_MAX_ENTRIES 64 /* absolute maximum number of entries per row, for all configurations */
  668. #define ZSTD_ROW_HASH_CACHE_MASK (ZSTD_ROW_HASH_CACHE_SIZE - 1)
  669. typedef U64 ZSTD_VecMask; /* Clarifies when we are interacting with a U64 representing a mask of matches */
  670. /* ZSTD_VecMask_next():
  671. * Starting from the LSB, returns the idx of the next non-zero bit.
  672. * Basically counting the nb of trailing zeroes.
  673. */
  674. static U32 ZSTD_VecMask_next(ZSTD_VecMask val) {
  675. assert(val != 0);
  676. # if defined(_MSC_VER) && defined(_WIN64)
  677. if (val != 0) {
  678. unsigned long r;
  679. _BitScanForward64(&r, val);
  680. return (U32)(r);
  681. } else {
  682. /* Should not reach this code path */
  683. __assume(0);
  684. }
  685. # elif (defined(__GNUC__) && ((__GNUC__ > 3) || ((__GNUC__ == 3) && (__GNUC_MINOR__ >= 4))))
  686. if (sizeof(size_t) == 4) {
  687. U32 mostSignificantWord = (U32)(val >> 32);
  688. U32 leastSignificantWord = (U32)val;
  689. if (leastSignificantWord == 0) {
  690. return 32 + (U32)__builtin_ctz(mostSignificantWord);
  691. } else {
  692. return (U32)__builtin_ctz(leastSignificantWord);
  693. }
  694. } else {
  695. return (U32)__builtin_ctzll(val);
  696. }
  697. # else
  698. /* Software ctz version: http://aggregate.org/MAGIC/#Trailing%20Zero%20Count
  699. * and: https://stackoverflow.com/questions/2709430/count-number-of-bits-in-a-64-bit-long-big-integer
  700. */
  701. val = ~val & (val - 1ULL); /* Lowest set bit mask */
  702. val = val - ((val >> 1) & 0x5555555555555555);
  703. val = (val & 0x3333333333333333ULL) + ((val >> 2) & 0x3333333333333333ULL);
  704. return (U32)((((val + (val >> 4)) & 0xF0F0F0F0F0F0F0FULL) * 0x101010101010101ULL) >> 56);
  705. # endif
  706. }
  707. /* ZSTD_rotateRight_*():
  708. * Rotates a bitfield to the right by "count" bits.
  709. * https://en.wikipedia.org/w/index.php?title=Circular_shift&oldid=991635599#Implementing_circular_shifts
  710. */
  711. FORCE_INLINE_TEMPLATE
  712. U64 ZSTD_rotateRight_U64(U64 const value, U32 count) {
  713. assert(count < 64);
  714. count &= 0x3F; /* for fickle pattern recognition */
  715. return (value >> count) | (U64)(value << ((0U - count) & 0x3F));
  716. }
  717. FORCE_INLINE_TEMPLATE
  718. U32 ZSTD_rotateRight_U32(U32 const value, U32 count) {
  719. assert(count < 32);
  720. count &= 0x1F; /* for fickle pattern recognition */
  721. return (value >> count) | (U32)(value << ((0U - count) & 0x1F));
  722. }
  723. FORCE_INLINE_TEMPLATE
  724. U16 ZSTD_rotateRight_U16(U16 const value, U32 count) {
  725. assert(count < 16);
  726. count &= 0x0F; /* for fickle pattern recognition */
  727. return (value >> count) | (U16)(value << ((0U - count) & 0x0F));
  728. }
  729. /* ZSTD_row_nextIndex():
  730. * Returns the next index to insert at within a tagTable row, and updates the "head"
  731. * value to reflect the update. Essentially cycles backwards from [0, {entries per row})
  732. */
  733. FORCE_INLINE_TEMPLATE U32 ZSTD_row_nextIndex(BYTE* const tagRow, U32 const rowMask) {
  734. U32 const next = (*tagRow - 1) & rowMask;
  735. *tagRow = (BYTE)next;
  736. return next;
  737. }
  738. /* ZSTD_isAligned():
  739. * Checks that a pointer is aligned to "align" bytes which must be a power of 2.
  740. */
  741. MEM_STATIC int ZSTD_isAligned(void const* ptr, size_t align) {
  742. assert((align & (align - 1)) == 0);
  743. return (((size_t)ptr) & (align - 1)) == 0;
  744. }
  745. /* ZSTD_row_prefetch():
  746. * Performs prefetching for the hashTable and tagTable at a given row.
  747. */
  748. FORCE_INLINE_TEMPLATE void ZSTD_row_prefetch(U32 const* hashTable, U16 const* tagTable, U32 const relRow, U32 const rowLog) {
  749. PREFETCH_L1(hashTable + relRow);
  750. if (rowLog >= 5) {
  751. PREFETCH_L1(hashTable + relRow + 16);
  752. /* Note: prefetching more of the hash table does not appear to be beneficial for 128-entry rows */
  753. }
  754. PREFETCH_L1(tagTable + relRow);
  755. if (rowLog == 6) {
  756. PREFETCH_L1(tagTable + relRow + 32);
  757. }
  758. assert(rowLog == 4 || rowLog == 5 || rowLog == 6);
  759. assert(ZSTD_isAligned(hashTable + relRow, 64)); /* prefetched hash row always 64-byte aligned */
  760. assert(ZSTD_isAligned(tagTable + relRow, (size_t)1 << rowLog)); /* prefetched tagRow sits on correct multiple of bytes (32,64,128) */
  761. }
  762. /* ZSTD_row_fillHashCache():
  763. * Fill up the hash cache starting at idx, prefetching up to ZSTD_ROW_HASH_CACHE_SIZE entries,
  764. * but not beyond iLimit.
  765. */
  766. FORCE_INLINE_TEMPLATE void ZSTD_row_fillHashCache(ZSTD_matchState_t* ms, const BYTE* base,
  767. U32 const rowLog, U32 const mls,
  768. U32 idx, const BYTE* const iLimit)
  769. {
  770. U32 const* const hashTable = ms->hashTable;
  771. U16 const* const tagTable = ms->tagTable;
  772. U32 const hashLog = ms->rowHashLog;
  773. U32 const maxElemsToPrefetch = (base + idx) > iLimit ? 0 : (U32)(iLimit - (base + idx) + 1);
  774. U32 const lim = idx + MIN(ZSTD_ROW_HASH_CACHE_SIZE, maxElemsToPrefetch);
  775. for (; idx < lim; ++idx) {
  776. U32 const hash = (U32)ZSTD_hashPtr(base + idx, hashLog + ZSTD_ROW_HASH_TAG_BITS, mls);
  777. U32 const row = (hash >> ZSTD_ROW_HASH_TAG_BITS) << rowLog;
  778. ZSTD_row_prefetch(hashTable, tagTable, row, rowLog);
  779. ms->hashCache[idx & ZSTD_ROW_HASH_CACHE_MASK] = hash;
  780. }
  781. DEBUGLOG(6, "ZSTD_row_fillHashCache(): [%u %u %u %u %u %u %u %u]", ms->hashCache[0], ms->hashCache[1],
  782. ms->hashCache[2], ms->hashCache[3], ms->hashCache[4],
  783. ms->hashCache[5], ms->hashCache[6], ms->hashCache[7]);
  784. }
  785. /* ZSTD_row_nextCachedHash():
  786. * Returns the hash of base + idx, and replaces the hash in the hash cache with the byte at
  787. * base + idx + ZSTD_ROW_HASH_CACHE_SIZE. Also prefetches the appropriate rows from hashTable and tagTable.
  788. */
  789. FORCE_INLINE_TEMPLATE U32 ZSTD_row_nextCachedHash(U32* cache, U32 const* hashTable,
  790. U16 const* tagTable, BYTE const* base,
  791. U32 idx, U32 const hashLog,
  792. U32 const rowLog, U32 const mls)
  793. {
  794. U32 const newHash = (U32)ZSTD_hashPtr(base+idx+ZSTD_ROW_HASH_CACHE_SIZE, hashLog + ZSTD_ROW_HASH_TAG_BITS, mls);
  795. U32 const row = (newHash >> ZSTD_ROW_HASH_TAG_BITS) << rowLog;
  796. ZSTD_row_prefetch(hashTable, tagTable, row, rowLog);
  797. { U32 const hash = cache[idx & ZSTD_ROW_HASH_CACHE_MASK];
  798. cache[idx & ZSTD_ROW_HASH_CACHE_MASK] = newHash;
  799. return hash;
  800. }
  801. }
  802. /* ZSTD_row_update_internalImpl():
  803. * Updates the hash table with positions starting from updateStartIdx until updateEndIdx.
  804. */
  805. FORCE_INLINE_TEMPLATE void ZSTD_row_update_internalImpl(ZSTD_matchState_t* ms,
  806. U32 updateStartIdx, U32 const updateEndIdx,
  807. U32 const mls, U32 const rowLog,
  808. U32 const rowMask, U32 const useCache)
  809. {
  810. U32* const hashTable = ms->hashTable;
  811. U16* const tagTable = ms->tagTable;
  812. U32 const hashLog = ms->rowHashLog;
  813. const BYTE* const base = ms->window.base;
  814. DEBUGLOG(6, "ZSTD_row_update_internalImpl(): updateStartIdx=%u, updateEndIdx=%u", updateStartIdx, updateEndIdx);
  815. for (; updateStartIdx < updateEndIdx; ++updateStartIdx) {
  816. U32 const hash = useCache ? ZSTD_row_nextCachedHash(ms->hashCache, hashTable, tagTable, base, updateStartIdx, hashLog, rowLog, mls)
  817. : (U32)ZSTD_hashPtr(base + updateStartIdx, hashLog + ZSTD_ROW_HASH_TAG_BITS, mls);
  818. U32 const relRow = (hash >> ZSTD_ROW_HASH_TAG_BITS) << rowLog;
  819. U32* const row = hashTable + relRow;
  820. BYTE* tagRow = (BYTE*)(tagTable + relRow); /* Though tagTable is laid out as a table of U16, each tag is only 1 byte.
  821. Explicit cast allows us to get exact desired position within each row */
  822. U32 const pos = ZSTD_row_nextIndex(tagRow, rowMask);
  823. assert(hash == ZSTD_hashPtr(base + updateStartIdx, hashLog + ZSTD_ROW_HASH_TAG_BITS, mls));
  824. ((BYTE*)tagRow)[pos + ZSTD_ROW_HASH_TAG_OFFSET] = hash & ZSTD_ROW_HASH_TAG_MASK;
  825. row[pos] = updateStartIdx;
  826. }
  827. }
  828. /* ZSTD_row_update_internal():
  829. * Inserts the byte at ip into the appropriate position in the hash table, and updates ms->nextToUpdate.
  830. * Skips sections of long matches as is necessary.
  831. */
  832. FORCE_INLINE_TEMPLATE void ZSTD_row_update_internal(ZSTD_matchState_t* ms, const BYTE* ip,
  833. U32 const mls, U32 const rowLog,
  834. U32 const rowMask, U32 const useCache)
  835. {
  836. U32 idx = ms->nextToUpdate;
  837. const BYTE* const base = ms->window.base;
  838. const U32 target = (U32)(ip - base);
  839. const U32 kSkipThreshold = 384;
  840. const U32 kMaxMatchStartPositionsToUpdate = 96;
  841. const U32 kMaxMatchEndPositionsToUpdate = 32;
  842. if (useCache) {
  843. /* Only skip positions when using hash cache, i.e.
  844. * if we are loading a dict, don't skip anything.
  845. * If we decide to skip, then we only update a set number
  846. * of positions at the beginning and end of the match.
  847. */
  848. if (UNLIKELY(target - idx > kSkipThreshold)) {
  849. U32 const bound = idx + kMaxMatchStartPositionsToUpdate;
  850. ZSTD_row_update_internalImpl(ms, idx, bound, mls, rowLog, rowMask, useCache);
  851. idx = target - kMaxMatchEndPositionsToUpdate;
  852. ZSTD_row_fillHashCache(ms, base, rowLog, mls, idx, ip+1);
  853. }
  854. }
  855. assert(target >= idx);
  856. ZSTD_row_update_internalImpl(ms, idx, target, mls, rowLog, rowMask, useCache);
  857. ms->nextToUpdate = target;
  858. }
  859. /* ZSTD_row_update():
  860. * External wrapper for ZSTD_row_update_internal(). Used for filling the hashtable during dictionary
  861. * processing.
  862. */
  863. void ZSTD_row_update(ZSTD_matchState_t* const ms, const BYTE* ip) {
  864. const U32 rowLog = BOUNDED(4, ms->cParams.searchLog, 6);
  865. const U32 rowMask = (1u << rowLog) - 1;
  866. const U32 mls = MIN(ms->cParams.minMatch, 6 /* mls caps out at 6 */);
  867. DEBUGLOG(5, "ZSTD_row_update(), rowLog=%u", rowLog);
  868. ZSTD_row_update_internal(ms, ip, mls, rowLog, rowMask, 0 /* dont use cache */);
  869. }
  870. #if defined(ZSTD_ARCH_X86_SSE2)
  871. FORCE_INLINE_TEMPLATE ZSTD_VecMask
  872. ZSTD_row_getSSEMask(int nbChunks, const BYTE* const src, const BYTE tag, const U32 head)
  873. {
  874. const __m128i comparisonMask = _mm_set1_epi8((char)tag);
  875. int matches[4] = {0};
  876. int i;
  877. assert(nbChunks == 1 || nbChunks == 2 || nbChunks == 4);
  878. for (i=0; i<nbChunks; i++) {
  879. const __m128i chunk = _mm_loadu_si128((const __m128i*)(const void*)(src + 16*i));
  880. const __m128i equalMask = _mm_cmpeq_epi8(chunk, comparisonMask);
  881. matches[i] = _mm_movemask_epi8(equalMask);
  882. }
  883. if (nbChunks == 1) return ZSTD_rotateRight_U16((U16)matches[0], head);
  884. if (nbChunks == 2) return ZSTD_rotateRight_U32((U32)matches[1] << 16 | (U32)matches[0], head);
  885. assert(nbChunks == 4);
  886. return ZSTD_rotateRight_U64((U64)matches[3] << 48 | (U64)matches[2] << 32 | (U64)matches[1] << 16 | (U64)matches[0], head);
  887. }
  888. #endif
  889. /* Returns a ZSTD_VecMask (U32) that has the nth bit set to 1 if the newly-computed "tag" matches
  890. * the hash at the nth position in a row of the tagTable.
  891. * Each row is a circular buffer beginning at the value of "head". So we must rotate the "matches" bitfield
  892. * to match up with the actual layout of the entries within the hashTable */
  893. FORCE_INLINE_TEMPLATE ZSTD_VecMask
  894. ZSTD_row_getMatchMask(const BYTE* const tagRow, const BYTE tag, const U32 head, const U32 rowEntries)
  895. {
  896. const BYTE* const src = tagRow + ZSTD_ROW_HASH_TAG_OFFSET;
  897. assert((rowEntries == 16) || (rowEntries == 32) || rowEntries == 64);
  898. assert(rowEntries <= ZSTD_ROW_HASH_MAX_ENTRIES);
  899. #if defined(ZSTD_ARCH_X86_SSE2)
  900. return ZSTD_row_getSSEMask(rowEntries / 16, src, tag, head);
  901. #else /* SW or NEON-LE */
  902. # if defined(ZSTD_ARCH_ARM_NEON)
  903. /* This NEON path only works for little endian - otherwise use SWAR below */
  904. if (MEM_isLittleEndian()) {
  905. if (rowEntries == 16) {
  906. const uint8x16_t chunk = vld1q_u8(src);
  907. const uint16x8_t equalMask = vreinterpretq_u16_u8(vceqq_u8(chunk, vdupq_n_u8(tag)));
  908. const uint16x8_t t0 = vshlq_n_u16(equalMask, 7);
  909. const uint32x4_t t1 = vreinterpretq_u32_u16(vsriq_n_u16(t0, t0, 14));
  910. const uint64x2_t t2 = vreinterpretq_u64_u32(vshrq_n_u32(t1, 14));
  911. const uint8x16_t t3 = vreinterpretq_u8_u64(vsraq_n_u64(t2, t2, 28));
  912. const U16 hi = (U16)vgetq_lane_u8(t3, 8);
  913. const U16 lo = (U16)vgetq_lane_u8(t3, 0);
  914. return ZSTD_rotateRight_U16((hi << 8) | lo, head);
  915. } else if (rowEntries == 32) {
  916. const uint16x8x2_t chunk = vld2q_u16((const U16*)(const void*)src);
  917. const uint8x16_t chunk0 = vreinterpretq_u8_u16(chunk.val[0]);
  918. const uint8x16_t chunk1 = vreinterpretq_u8_u16(chunk.val[1]);
  919. const uint8x16_t equalMask0 = vceqq_u8(chunk0, vdupq_n_u8(tag));
  920. const uint8x16_t equalMask1 = vceqq_u8(chunk1, vdupq_n_u8(tag));
  921. const int8x8_t pack0 = vqmovn_s16(vreinterpretq_s16_u8(equalMask0));
  922. const int8x8_t pack1 = vqmovn_s16(vreinterpretq_s16_u8(equalMask1));
  923. const uint8x8_t t0 = vreinterpret_u8_s8(pack0);
  924. const uint8x8_t t1 = vreinterpret_u8_s8(pack1);
  925. const uint8x8_t t2 = vsri_n_u8(t1, t0, 2);
  926. const uint8x8x2_t t3 = vuzp_u8(t2, t0);
  927. const uint8x8_t t4 = vsri_n_u8(t3.val[1], t3.val[0], 4);
  928. const U32 matches = vget_lane_u32(vreinterpret_u32_u8(t4), 0);
  929. return ZSTD_rotateRight_U32(matches, head);
  930. } else { /* rowEntries == 64 */
  931. const uint8x16x4_t chunk = vld4q_u8(src);
  932. const uint8x16_t dup = vdupq_n_u8(tag);
  933. const uint8x16_t cmp0 = vceqq_u8(chunk.val[0], dup);
  934. const uint8x16_t cmp1 = vceqq_u8(chunk.val[1], dup);
  935. const uint8x16_t cmp2 = vceqq_u8(chunk.val[2], dup);
  936. const uint8x16_t cmp3 = vceqq_u8(chunk.val[3], dup);
  937. const uint8x16_t t0 = vsriq_n_u8(cmp1, cmp0, 1);
  938. const uint8x16_t t1 = vsriq_n_u8(cmp3, cmp2, 1);
  939. const uint8x16_t t2 = vsriq_n_u8(t1, t0, 2);
  940. const uint8x16_t t3 = vsriq_n_u8(t2, t2, 4);
  941. const uint8x8_t t4 = vshrn_n_u16(vreinterpretq_u16_u8(t3), 4);
  942. const U64 matches = vget_lane_u64(vreinterpret_u64_u8(t4), 0);
  943. return ZSTD_rotateRight_U64(matches, head);
  944. }
  945. }
  946. # endif /* ZSTD_ARCH_ARM_NEON */
  947. /* SWAR */
  948. { const size_t chunkSize = sizeof(size_t);
  949. const size_t shiftAmount = ((chunkSize * 8) - chunkSize);
  950. const size_t xFF = ~((size_t)0);
  951. const size_t x01 = xFF / 0xFF;
  952. const size_t x80 = x01 << 7;
  953. const size_t splatChar = tag * x01;
  954. ZSTD_VecMask matches = 0;
  955. int i = rowEntries - chunkSize;
  956. assert((sizeof(size_t) == 4) || (sizeof(size_t) == 8));
  957. if (MEM_isLittleEndian()) { /* runtime check so have two loops */
  958. const size_t extractMagic = (xFF / 0x7F) >> chunkSize;
  959. do {
  960. size_t chunk = MEM_readST(&src[i]);
  961. chunk ^= splatChar;
  962. chunk = (((chunk | x80) - x01) | chunk) & x80;
  963. matches <<= chunkSize;
  964. matches |= (chunk * extractMagic) >> shiftAmount;
  965. i -= chunkSize;
  966. } while (i >= 0);
  967. } else { /* big endian: reverse bits during extraction */
  968. const size_t msb = xFF ^ (xFF >> 1);
  969. const size_t extractMagic = (msb / 0x1FF) | msb;
  970. do {
  971. size_t chunk = MEM_readST(&src[i]);
  972. chunk ^= splatChar;
  973. chunk = (((chunk | x80) - x01) | chunk) & x80;
  974. matches <<= chunkSize;
  975. matches |= ((chunk >> 7) * extractMagic) >> shiftAmount;
  976. i -= chunkSize;
  977. } while (i >= 0);
  978. }
  979. matches = ~matches;
  980. if (rowEntries == 16) {
  981. return ZSTD_rotateRight_U16((U16)matches, head);
  982. } else if (rowEntries == 32) {
  983. return ZSTD_rotateRight_U32((U32)matches, head);
  984. } else {
  985. return ZSTD_rotateRight_U64((U64)matches, head);
  986. }
  987. }
  988. #endif
  989. }
  990. /* The high-level approach of the SIMD row based match finder is as follows:
  991. * - Figure out where to insert the new entry:
  992. * - Generate a hash from a byte along with an additional 1-byte "short hash". The additional byte is our "tag"
  993. * - The hashTable is effectively split into groups or "rows" of 16 or 32 entries of U32, and the hash determines
  994. * which row to insert into.
  995. * - Determine the correct position within the row to insert the entry into. Each row of 16 or 32 can
  996. * be considered as a circular buffer with a "head" index that resides in the tagTable.
  997. * - Also insert the "tag" into the equivalent row and position in the tagTable.
  998. * - Note: The tagTable has 17 or 33 1-byte entries per row, due to 16 or 32 tags, and 1 "head" entry.
  999. * The 17 or 33 entry rows are spaced out to occur every 32 or 64 bytes, respectively,
  1000. * for alignment/performance reasons, leaving some bytes unused.
  1001. * - Use SIMD to efficiently compare the tags in the tagTable to the 1-byte "short hash" and
  1002. * generate a bitfield that we can cycle through to check the collisions in the hash table.
  1003. * - Pick the longest match.
  1004. */
  1005. FORCE_INLINE_TEMPLATE
  1006. size_t ZSTD_RowFindBestMatch(
  1007. ZSTD_matchState_t* ms,
  1008. const BYTE* const ip, const BYTE* const iLimit,
  1009. size_t* offsetPtr,
  1010. const U32 mls, const ZSTD_dictMode_e dictMode,
  1011. const U32 rowLog)
  1012. {
  1013. U32* const hashTable = ms->hashTable;
  1014. U16* const tagTable = ms->tagTable;
  1015. U32* const hashCache = ms->hashCache;
  1016. const U32 hashLog = ms->rowHashLog;
  1017. const ZSTD_compressionParameters* const cParams = &ms->cParams;
  1018. const BYTE* const base = ms->window.base;
  1019. const BYTE* const dictBase = ms->window.dictBase;
  1020. const U32 dictLimit = ms->window.dictLimit;
  1021. const BYTE* const prefixStart = base + dictLimit;
  1022. const BYTE* const dictEnd = dictBase + dictLimit;
  1023. const U32 curr = (U32)(ip-base);
  1024. const U32 maxDistance = 1U << cParams->windowLog;
  1025. const U32 lowestValid = ms->window.lowLimit;
  1026. const U32 withinMaxDistance = (curr - lowestValid > maxDistance) ? curr - maxDistance : lowestValid;
  1027. const U32 isDictionary = (ms->loadedDictEnd != 0);
  1028. const U32 lowLimit = isDictionary ? lowestValid : withinMaxDistance;
  1029. const U32 rowEntries = (1U << rowLog);
  1030. const U32 rowMask = rowEntries - 1;
  1031. const U32 cappedSearchLog = MIN(cParams->searchLog, rowLog); /* nb of searches is capped at nb entries per row */
  1032. U32 nbAttempts = 1U << cappedSearchLog;
  1033. size_t ml=4-1;
  1034. /* DMS/DDS variables that may be referenced laster */
  1035. const ZSTD_matchState_t* const dms = ms->dictMatchState;
  1036. /* Initialize the following variables to satisfy static analyzer */
  1037. size_t ddsIdx = 0;
  1038. U32 ddsExtraAttempts = 0; /* cctx hash tables are limited in searches, but allow extra searches into DDS */
  1039. U32 dmsTag = 0;
  1040. U32* dmsRow = NULL;
  1041. BYTE* dmsTagRow = NULL;
  1042. if (dictMode == ZSTD_dedicatedDictSearch) {
  1043. const U32 ddsHashLog = dms->cParams.hashLog - ZSTD_LAZY_DDSS_BUCKET_LOG;
  1044. { /* Prefetch DDS hashtable entry */
  1045. ddsIdx = ZSTD_hashPtr(ip, ddsHashLog, mls) << ZSTD_LAZY_DDSS_BUCKET_LOG;
  1046. PREFETCH_L1(&dms->hashTable[ddsIdx]);
  1047. }
  1048. ddsExtraAttempts = cParams->searchLog > rowLog ? 1U << (cParams->searchLog - rowLog) : 0;
  1049. }
  1050. if (dictMode == ZSTD_dictMatchState) {
  1051. /* Prefetch DMS rows */
  1052. U32* const dmsHashTable = dms->hashTable;
  1053. U16* const dmsTagTable = dms->tagTable;
  1054. U32 const dmsHash = (U32)ZSTD_hashPtr(ip, dms->rowHashLog + ZSTD_ROW_HASH_TAG_BITS, mls);
  1055. U32 const dmsRelRow = (dmsHash >> ZSTD_ROW_HASH_TAG_BITS) << rowLog;
  1056. dmsTag = dmsHash & ZSTD_ROW_HASH_TAG_MASK;
  1057. dmsTagRow = (BYTE*)(dmsTagTable + dmsRelRow);
  1058. dmsRow = dmsHashTable + dmsRelRow;
  1059. ZSTD_row_prefetch(dmsHashTable, dmsTagTable, dmsRelRow, rowLog);
  1060. }
  1061. /* Update the hashTable and tagTable up to (but not including) ip */
  1062. ZSTD_row_update_internal(ms, ip, mls, rowLog, rowMask, 1 /* useCache */);
  1063. { /* Get the hash for ip, compute the appropriate row */
  1064. U32 const hash = ZSTD_row_nextCachedHash(hashCache, hashTable, tagTable, base, curr, hashLog, rowLog, mls);
  1065. U32 const relRow = (hash >> ZSTD_ROW_HASH_TAG_BITS) << rowLog;
  1066. U32 const tag = hash & ZSTD_ROW_HASH_TAG_MASK;
  1067. U32* const row = hashTable + relRow;
  1068. BYTE* tagRow = (BYTE*)(tagTable + relRow);
  1069. U32 const head = *tagRow & rowMask;
  1070. U32 matchBuffer[ZSTD_ROW_HASH_MAX_ENTRIES];
  1071. size_t numMatches = 0;
  1072. size_t currMatch = 0;
  1073. ZSTD_VecMask matches = ZSTD_row_getMatchMask(tagRow, (BYTE)tag, head, rowEntries);
  1074. /* Cycle through the matches and prefetch */
  1075. for (; (matches > 0) && (nbAttempts > 0); --nbAttempts, matches &= (matches - 1)) {
  1076. U32 const matchPos = (head + ZSTD_VecMask_next(matches)) & rowMask;
  1077. U32 const matchIndex = row[matchPos];
  1078. assert(numMatches < rowEntries);
  1079. if (matchIndex < lowLimit)
  1080. break;
  1081. if ((dictMode != ZSTD_extDict) || matchIndex >= dictLimit) {
  1082. PREFETCH_L1(base + matchIndex);
  1083. } else {
  1084. PREFETCH_L1(dictBase + matchIndex);
  1085. }
  1086. matchBuffer[numMatches++] = matchIndex;
  1087. }
  1088. /* Speed opt: insert current byte into hashtable too. This allows us to avoid one iteration of the loop
  1089. in ZSTD_row_update_internal() at the next search. */
  1090. {
  1091. U32 const pos = ZSTD_row_nextIndex(tagRow, rowMask);
  1092. tagRow[pos + ZSTD_ROW_HASH_TAG_OFFSET] = (BYTE)tag;
  1093. row[pos] = ms->nextToUpdate++;
  1094. }
  1095. /* Return the longest match */
  1096. for (; currMatch < numMatches; ++currMatch) {
  1097. U32 const matchIndex = matchBuffer[currMatch];
  1098. size_t currentMl=0;
  1099. assert(matchIndex < curr);
  1100. assert(matchIndex >= lowLimit);
  1101. if ((dictMode != ZSTD_extDict) || matchIndex >= dictLimit) {
  1102. const BYTE* const match = base + matchIndex;
  1103. assert(matchIndex >= dictLimit); /* ensures this is true if dictMode != ZSTD_extDict */
  1104. if (match[ml] == ip[ml]) /* potentially better */
  1105. currentMl = ZSTD_count(ip, match, iLimit);
  1106. } else {
  1107. const BYTE* const match = dictBase + matchIndex;
  1108. assert(match+4 <= dictEnd);
  1109. if (MEM_read32(match) == MEM_read32(ip)) /* assumption : matchIndex <= dictLimit-4 (by table construction) */
  1110. currentMl = ZSTD_count_2segments(ip+4, match+4, iLimit, dictEnd, prefixStart) + 4;
  1111. }
  1112. /* Save best solution */
  1113. if (currentMl > ml) {
  1114. ml = currentMl;
  1115. *offsetPtr = STORE_OFFSET(curr - matchIndex);
  1116. if (ip+currentMl == iLimit) break; /* best possible, avoids read overflow on next attempt */
  1117. }
  1118. }
  1119. }
  1120. assert(nbAttempts <= (1U << ZSTD_SEARCHLOG_MAX)); /* Check we haven't underflowed. */
  1121. if (dictMode == ZSTD_dedicatedDictSearch) {
  1122. ml = ZSTD_dedicatedDictSearch_lazy_search(offsetPtr, ml, nbAttempts + ddsExtraAttempts, dms,
  1123. ip, iLimit, prefixStart, curr, dictLimit, ddsIdx);
  1124. } else if (dictMode == ZSTD_dictMatchState) {
  1125. /* TODO: Measure and potentially add prefetching to DMS */
  1126. const U32 dmsLowestIndex = dms->window.dictLimit;
  1127. const BYTE* const dmsBase = dms->window.base;
  1128. const BYTE* const dmsEnd = dms->window.nextSrc;
  1129. const U32 dmsSize = (U32)(dmsEnd - dmsBase);
  1130. const U32 dmsIndexDelta = dictLimit - dmsSize;
  1131. { U32 const head = *dmsTagRow & rowMask;
  1132. U32 matchBuffer[ZSTD_ROW_HASH_MAX_ENTRIES];
  1133. size_t numMatches = 0;
  1134. size_t currMatch = 0;
  1135. ZSTD_VecMask matches = ZSTD_row_getMatchMask(dmsTagRow, (BYTE)dmsTag, head, rowEntries);
  1136. for (; (matches > 0) && (nbAttempts > 0); --nbAttempts, matches &= (matches - 1)) {
  1137. U32 const matchPos = (head + ZSTD_VecMask_next(matches)) & rowMask;
  1138. U32 const matchIndex = dmsRow[matchPos];
  1139. if (matchIndex < dmsLowestIndex)
  1140. break;
  1141. PREFETCH_L1(dmsBase + matchIndex);
  1142. matchBuffer[numMatches++] = matchIndex;
  1143. }
  1144. /* Return the longest match */
  1145. for (; currMatch < numMatches; ++currMatch) {
  1146. U32 const matchIndex = matchBuffer[currMatch];
  1147. size_t currentMl=0;
  1148. assert(matchIndex >= dmsLowestIndex);
  1149. assert(matchIndex < curr);
  1150. { const BYTE* const match = dmsBase + matchIndex;
  1151. assert(match+4 <= dmsEnd);
  1152. if (MEM_read32(match) == MEM_read32(ip))
  1153. currentMl = ZSTD_count_2segments(ip+4, match+4, iLimit, dmsEnd, prefixStart) + 4;
  1154. }
  1155. if (currentMl > ml) {
  1156. ml = currentMl;
  1157. assert(curr > matchIndex + dmsIndexDelta);
  1158. *offsetPtr = STORE_OFFSET(curr - (matchIndex + dmsIndexDelta));
  1159. if (ip+currentMl == iLimit) break;
  1160. }
  1161. }
  1162. }
  1163. }
  1164. return ml;
  1165. }
  1166. typedef size_t (*searchMax_f)(
  1167. ZSTD_matchState_t* ms,
  1168. const BYTE* ip, const BYTE* iLimit, size_t* offsetPtr);
  1169. /**
  1170. * This struct contains the functions necessary for lazy to search.
  1171. * Currently, that is only searchMax. However, it is still valuable to have the
  1172. * VTable because this makes it easier to add more functions to the VTable later.
  1173. *
  1174. * TODO: The start of the search function involves loading and calculating a
  1175. * bunch of constants from the ZSTD_matchState_t. These computations could be
  1176. * done in an initialization function, and saved somewhere in the match state.
  1177. * Then we could pass a pointer to the saved state instead of the match state,
  1178. * and avoid duplicate computations.
  1179. *
  1180. * TODO: Move the match re-winding into searchMax. This improves compression
  1181. * ratio, and unlocks further simplifications with the next TODO.
  1182. *
  1183. * TODO: Try moving the repcode search into searchMax. After the re-winding
  1184. * and repcode search are in searchMax, there is no more logic in the match
  1185. * finder loop that requires knowledge about the dictMode. So we should be
  1186. * able to avoid force inlining it, and we can join the extDict loop with
  1187. * the single segment loop. It should go in searchMax instead of its own
  1188. * function to avoid having multiple virtual function calls per search.
  1189. */
  1190. typedef struct {
  1191. searchMax_f searchMax;
  1192. } ZSTD_LazyVTable;
  1193. #define GEN_ZSTD_BT_VTABLE(dictMode, mls) \
  1194. static size_t ZSTD_BtFindBestMatch_##dictMode##_##mls( \
  1195. ZSTD_matchState_t* ms, \
  1196. const BYTE* ip, const BYTE* const iLimit, \
  1197. size_t* offsetPtr) \
  1198. { \
  1199. assert(MAX(4, MIN(6, ms->cParams.minMatch)) == mls); \
  1200. return ZSTD_BtFindBestMatch(ms, ip, iLimit, offsetPtr, mls, ZSTD_##dictMode); \
  1201. } \
  1202. static const ZSTD_LazyVTable ZSTD_BtVTable_##dictMode##_##mls = { \
  1203. ZSTD_BtFindBestMatch_##dictMode##_##mls \
  1204. };
  1205. #define GEN_ZSTD_HC_VTABLE(dictMode, mls) \
  1206. static size_t ZSTD_HcFindBestMatch_##dictMode##_##mls( \
  1207. ZSTD_matchState_t* ms, \
  1208. const BYTE* ip, const BYTE* const iLimit, \
  1209. size_t* offsetPtr) \
  1210. { \
  1211. assert(MAX(4, MIN(6, ms->cParams.minMatch)) == mls); \
  1212. return ZSTD_HcFindBestMatch(ms, ip, iLimit, offsetPtr, mls, ZSTD_##dictMode); \
  1213. } \
  1214. static const ZSTD_LazyVTable ZSTD_HcVTable_##dictMode##_##mls = { \
  1215. ZSTD_HcFindBestMatch_##dictMode##_##mls \
  1216. };
  1217. #define GEN_ZSTD_ROW_VTABLE(dictMode, mls, rowLog) \
  1218. static size_t ZSTD_RowFindBestMatch_##dictMode##_##mls##_##rowLog( \
  1219. ZSTD_matchState_t* ms, \
  1220. const BYTE* ip, const BYTE* const iLimit, \
  1221. size_t* offsetPtr) \
  1222. { \
  1223. assert(MAX(4, MIN(6, ms->cParams.minMatch)) == mls); \
  1224. assert(MAX(4, MIN(6, ms->cParams.searchLog)) == rowLog); \
  1225. return ZSTD_RowFindBestMatch(ms, ip, iLimit, offsetPtr, mls, ZSTD_##dictMode, rowLog); \
  1226. } \
  1227. static const ZSTD_LazyVTable ZSTD_RowVTable_##dictMode##_##mls##_##rowLog = { \
  1228. ZSTD_RowFindBestMatch_##dictMode##_##mls##_##rowLog \
  1229. };
  1230. #define ZSTD_FOR_EACH_ROWLOG(X, dictMode, mls) \
  1231. X(dictMode, mls, 4) \
  1232. X(dictMode, mls, 5) \
  1233. X(dictMode, mls, 6)
  1234. #define ZSTD_FOR_EACH_MLS_ROWLOG(X, dictMode) \
  1235. ZSTD_FOR_EACH_ROWLOG(X, dictMode, 4) \
  1236. ZSTD_FOR_EACH_ROWLOG(X, dictMode, 5) \
  1237. ZSTD_FOR_EACH_ROWLOG(X, dictMode, 6)
  1238. #define ZSTD_FOR_EACH_MLS(X, dictMode) \
  1239. X(dictMode, 4) \
  1240. X(dictMode, 5) \
  1241. X(dictMode, 6)
  1242. #define ZSTD_FOR_EACH_DICT_MODE(X, ...) \
  1243. X(__VA_ARGS__, noDict) \
  1244. X(__VA_ARGS__, extDict) \
  1245. X(__VA_ARGS__, dictMatchState) \
  1246. X(__VA_ARGS__, dedicatedDictSearch)
  1247. /* Generate Row VTables for each combination of (dictMode, mls, rowLog) */
  1248. ZSTD_FOR_EACH_DICT_MODE(ZSTD_FOR_EACH_MLS_ROWLOG, GEN_ZSTD_ROW_VTABLE)
  1249. /* Generate Binary Tree VTables for each combination of (dictMode, mls) */
  1250. ZSTD_FOR_EACH_DICT_MODE(ZSTD_FOR_EACH_MLS, GEN_ZSTD_BT_VTABLE)
  1251. /* Generate Hash Chain VTables for each combination of (dictMode, mls) */
  1252. ZSTD_FOR_EACH_DICT_MODE(ZSTD_FOR_EACH_MLS, GEN_ZSTD_HC_VTABLE)
  1253. #define GEN_ZSTD_BT_VTABLE_ARRAY(dictMode) \
  1254. { \
  1255. &ZSTD_BtVTable_##dictMode##_4, \
  1256. &ZSTD_BtVTable_##dictMode##_5, \
  1257. &ZSTD_BtVTable_##dictMode##_6 \
  1258. }
  1259. #define GEN_ZSTD_HC_VTABLE_ARRAY(dictMode) \
  1260. { \
  1261. &ZSTD_HcVTable_##dictMode##_4, \
  1262. &ZSTD_HcVTable_##dictMode##_5, \
  1263. &ZSTD_HcVTable_##dictMode##_6 \
  1264. }
  1265. #define GEN_ZSTD_ROW_VTABLE_ARRAY_(dictMode, mls) \
  1266. { \
  1267. &ZSTD_RowVTable_##dictMode##_##mls##_4, \
  1268. &ZSTD_RowVTable_##dictMode##_##mls##_5, \
  1269. &ZSTD_RowVTable_##dictMode##_##mls##_6 \
  1270. }
  1271. #define GEN_ZSTD_ROW_VTABLE_ARRAY(dictMode) \
  1272. { \
  1273. GEN_ZSTD_ROW_VTABLE_ARRAY_(dictMode, 4), \
  1274. GEN_ZSTD_ROW_VTABLE_ARRAY_(dictMode, 5), \
  1275. GEN_ZSTD_ROW_VTABLE_ARRAY_(dictMode, 6) \
  1276. }
  1277. #define GEN_ZSTD_VTABLE_ARRAY(X) \
  1278. { \
  1279. X(noDict), \
  1280. X(extDict), \
  1281. X(dictMatchState), \
  1282. X(dedicatedDictSearch) \
  1283. }
  1284. /* *******************************
  1285. * Common parser - lazy strategy
  1286. *********************************/
  1287. typedef enum { search_hashChain=0, search_binaryTree=1, search_rowHash=2 } searchMethod_e;
  1288. /**
  1289. * This table is indexed first by the four ZSTD_dictMode_e values, and then
  1290. * by the two searchMethod_e values. NULLs are placed for configurations
  1291. * that should never occur (extDict modes go to the other implementation
  1292. * below and there is no DDSS for binary tree search yet).
  1293. */
  1294. static ZSTD_LazyVTable const*
  1295. ZSTD_selectLazyVTable(ZSTD_matchState_t const* ms, searchMethod_e searchMethod, ZSTD_dictMode_e dictMode)
  1296. {
  1297. /* Fill the Hc/Bt VTable arrays with the right functions for the (dictMode, mls) combination. */
  1298. ZSTD_LazyVTable const* const hcVTables[4][3] = GEN_ZSTD_VTABLE_ARRAY(GEN_ZSTD_HC_VTABLE_ARRAY);
  1299. ZSTD_LazyVTable const* const btVTables[4][3] = GEN_ZSTD_VTABLE_ARRAY(GEN_ZSTD_BT_VTABLE_ARRAY);
  1300. /* Fill the Row VTable array with the right functions for the (dictMode, mls, rowLog) combination. */
  1301. ZSTD_LazyVTable const* const rowVTables[4][3][3] = GEN_ZSTD_VTABLE_ARRAY(GEN_ZSTD_ROW_VTABLE_ARRAY);
  1302. U32 const mls = MAX(4, MIN(6, ms->cParams.minMatch));
  1303. U32 const rowLog = MAX(4, MIN(6, ms->cParams.searchLog));
  1304. switch (searchMethod) {
  1305. case search_hashChain:
  1306. return hcVTables[dictMode][mls - 4];
  1307. case search_binaryTree:
  1308. return btVTables[dictMode][mls - 4];
  1309. case search_rowHash:
  1310. return rowVTables[dictMode][mls - 4][rowLog - 4];
  1311. default:
  1312. return NULL;
  1313. }
  1314. }
  1315. FORCE_INLINE_TEMPLATE size_t
  1316. ZSTD_compressBlock_lazy_generic(
  1317. ZSTD_matchState_t* ms, seqStore_t* seqStore,
  1318. U32 rep[ZSTD_REP_NUM],
  1319. const void* src, size_t srcSize,
  1320. const searchMethod_e searchMethod, const U32 depth,
  1321. ZSTD_dictMode_e const dictMode)
  1322. {
  1323. const BYTE* const istart = (const BYTE*)src;
  1324. const BYTE* ip = istart;
  1325. const BYTE* anchor = istart;
  1326. const BYTE* const iend = istart + srcSize;
  1327. const BYTE* const ilimit = (searchMethod == search_rowHash) ? iend - 8 - ZSTD_ROW_HASH_CACHE_SIZE : iend - 8;
  1328. const BYTE* const base = ms->window.base;
  1329. const U32 prefixLowestIndex = ms->window.dictLimit;
  1330. const BYTE* const prefixLowest = base + prefixLowestIndex;
  1331. searchMax_f const searchMax = ZSTD_selectLazyVTable(ms, searchMethod, dictMode)->searchMax;
  1332. U32 offset_1 = rep[0], offset_2 = rep[1], savedOffset=0;
  1333. const int isDMS = dictMode == ZSTD_dictMatchState;
  1334. const int isDDS = dictMode == ZSTD_dedicatedDictSearch;
  1335. const int isDxS = isDMS || isDDS;
  1336. const ZSTD_matchState_t* const dms = ms->dictMatchState;
  1337. const U32 dictLowestIndex = isDxS ? dms->window.dictLimit : 0;
  1338. const BYTE* const dictBase = isDxS ? dms->window.base : NULL;
  1339. const BYTE* const dictLowest = isDxS ? dictBase + dictLowestIndex : NULL;
  1340. const BYTE* const dictEnd = isDxS ? dms->window.nextSrc : NULL;
  1341. const U32 dictIndexDelta = isDxS ?
  1342. prefixLowestIndex - (U32)(dictEnd - dictBase) :
  1343. 0;
  1344. const U32 dictAndPrefixLength = (U32)((ip - prefixLowest) + (dictEnd - dictLowest));
  1345. assert(searchMax != NULL);
  1346. DEBUGLOG(5, "ZSTD_compressBlock_lazy_generic (dictMode=%u) (searchFunc=%u)", (U32)dictMode, (U32)searchMethod);
  1347. ip += (dictAndPrefixLength == 0);
  1348. if (dictMode == ZSTD_noDict) {
  1349. U32 const curr = (U32)(ip - base);
  1350. U32 const windowLow = ZSTD_getLowestPrefixIndex(ms, curr, ms->cParams.windowLog);
  1351. U32 const maxRep = curr - windowLow;
  1352. if (offset_2 > maxRep) savedOffset = offset_2, offset_2 = 0;
  1353. if (offset_1 > maxRep) savedOffset = offset_1, offset_1 = 0;
  1354. }
  1355. if (isDxS) {
  1356. /* dictMatchState repCode checks don't currently handle repCode == 0
  1357. * disabling. */
  1358. assert(offset_1 <= dictAndPrefixLength);
  1359. assert(offset_2 <= dictAndPrefixLength);
  1360. }
  1361. if (searchMethod == search_rowHash) {
  1362. const U32 rowLog = MAX(4, MIN(6, ms->cParams.searchLog));
  1363. ZSTD_row_fillHashCache(ms, base, rowLog,
  1364. MIN(ms->cParams.minMatch, 6 /* mls caps out at 6 */),
  1365. ms->nextToUpdate, ilimit);
  1366. }
  1367. /* Match Loop */
  1368. #if defined(__GNUC__) && defined(__x86_64__)
  1369. /* I've measured random a 5% speed loss on levels 5 & 6 (greedy) when the
  1370. * code alignment is perturbed. To fix the instability align the loop on 32-bytes.
  1371. */
  1372. __asm__(".p2align 5");
  1373. #endif
  1374. while (ip < ilimit) {
  1375. size_t matchLength=0;
  1376. size_t offcode=STORE_REPCODE_1;
  1377. const BYTE* start=ip+1;
  1378. DEBUGLOG(7, "search baseline (depth 0)");
  1379. /* check repCode */
  1380. if (isDxS) {
  1381. const U32 repIndex = (U32)(ip - base) + 1 - offset_1;
  1382. const BYTE* repMatch = ((dictMode == ZSTD_dictMatchState || dictMode == ZSTD_dedicatedDictSearch)
  1383. && repIndex < prefixLowestIndex) ?
  1384. dictBase + (repIndex - dictIndexDelta) :
  1385. base + repIndex;
  1386. if (((U32)((prefixLowestIndex-1) - repIndex) >= 3 /* intentional underflow */)
  1387. && (MEM_read32(repMatch) == MEM_read32(ip+1)) ) {
  1388. const BYTE* repMatchEnd = repIndex < prefixLowestIndex ? dictEnd : iend;
  1389. matchLength = ZSTD_count_2segments(ip+1+4, repMatch+4, iend, repMatchEnd, prefixLowest) + 4;
  1390. if (depth==0) goto _storeSequence;
  1391. }
  1392. }
  1393. if ( dictMode == ZSTD_noDict
  1394. && ((offset_1 > 0) & (MEM_read32(ip+1-offset_1) == MEM_read32(ip+1)))) {
  1395. matchLength = ZSTD_count(ip+1+4, ip+1+4-offset_1, iend) + 4;
  1396. if (depth==0) goto _storeSequence;
  1397. }
  1398. /* first search (depth 0) */
  1399. { size_t offsetFound = 999999999;
  1400. size_t const ml2 = searchMax(ms, ip, iend, &offsetFound);
  1401. if (ml2 > matchLength)
  1402. matchLength = ml2, start = ip, offcode=offsetFound;
  1403. }
  1404. if (matchLength < 4) {
  1405. ip += ((ip-anchor) >> kSearchStrength) + 1; /* jump faster over incompressible sections */
  1406. continue;
  1407. }
  1408. /* let's try to find a better solution */
  1409. if (depth>=1)
  1410. while (ip<ilimit) {
  1411. DEBUGLOG(7, "search depth 1");
  1412. ip ++;
  1413. if ( (dictMode == ZSTD_noDict)
  1414. && (offcode) && ((offset_1>0) & (MEM_read32(ip) == MEM_read32(ip - offset_1)))) {
  1415. size_t const mlRep = ZSTD_count(ip+4, ip+4-offset_1, iend) + 4;
  1416. int const gain2 = (int)(mlRep * 3);
  1417. int const gain1 = (int)(matchLength*3 - ZSTD_highbit32((U32)STORED_TO_OFFBASE(offcode)) + 1);
  1418. if ((mlRep >= 4) && (gain2 > gain1))
  1419. matchLength = mlRep, offcode = STORE_REPCODE_1, start = ip;
  1420. }
  1421. if (isDxS) {
  1422. const U32 repIndex = (U32)(ip - base) - offset_1;
  1423. const BYTE* repMatch = repIndex < prefixLowestIndex ?
  1424. dictBase + (repIndex - dictIndexDelta) :
  1425. base + repIndex;
  1426. if (((U32)((prefixLowestIndex-1) - repIndex) >= 3 /* intentional underflow */)
  1427. && (MEM_read32(repMatch) == MEM_read32(ip)) ) {
  1428. const BYTE* repMatchEnd = repIndex < prefixLowestIndex ? dictEnd : iend;
  1429. size_t const mlRep = ZSTD_count_2segments(ip+4, repMatch+4, iend, repMatchEnd, prefixLowest) + 4;
  1430. int const gain2 = (int)(mlRep * 3);
  1431. int const gain1 = (int)(matchLength*3 - ZSTD_highbit32((U32)STORED_TO_OFFBASE(offcode)) + 1);
  1432. if ((mlRep >= 4) && (gain2 > gain1))
  1433. matchLength = mlRep, offcode = STORE_REPCODE_1, start = ip;
  1434. }
  1435. }
  1436. { size_t offset2=999999999;
  1437. size_t const ml2 = searchMax(ms, ip, iend, &offset2);
  1438. int const gain2 = (int)(ml2*4 - ZSTD_highbit32((U32)STORED_TO_OFFBASE(offset2))); /* raw approx */
  1439. int const gain1 = (int)(matchLength*4 - ZSTD_highbit32((U32)STORED_TO_OFFBASE(offcode)) + 4);
  1440. if ((ml2 >= 4) && (gain2 > gain1)) {
  1441. matchLength = ml2, offcode = offset2, start = ip;
  1442. continue; /* search a better one */
  1443. } }
  1444. /* let's find an even better one */
  1445. if ((depth==2) && (ip<ilimit)) {
  1446. DEBUGLOG(7, "search depth 2");
  1447. ip ++;
  1448. if ( (dictMode == ZSTD_noDict)
  1449. && (offcode) && ((offset_1>0) & (MEM_read32(ip) == MEM_read32(ip - offset_1)))) {
  1450. size_t const mlRep = ZSTD_count(ip+4, ip+4-offset_1, iend) + 4;
  1451. int const gain2 = (int)(mlRep * 4);
  1452. int const gain1 = (int)(matchLength*4 - ZSTD_highbit32((U32)STORED_TO_OFFBASE(offcode)) + 1);
  1453. if ((mlRep >= 4) && (gain2 > gain1))
  1454. matchLength = mlRep, offcode = STORE_REPCODE_1, start = ip;
  1455. }
  1456. if (isDxS) {
  1457. const U32 repIndex = (U32)(ip - base) - offset_1;
  1458. const BYTE* repMatch = repIndex < prefixLowestIndex ?
  1459. dictBase + (repIndex - dictIndexDelta) :
  1460. base + repIndex;
  1461. if (((U32)((prefixLowestIndex-1) - repIndex) >= 3 /* intentional underflow */)
  1462. && (MEM_read32(repMatch) == MEM_read32(ip)) ) {
  1463. const BYTE* repMatchEnd = repIndex < prefixLowestIndex ? dictEnd : iend;
  1464. size_t const mlRep = ZSTD_count_2segments(ip+4, repMatch+4, iend, repMatchEnd, prefixLowest) + 4;
  1465. int const gain2 = (int)(mlRep * 4);
  1466. int const gain1 = (int)(matchLength*4 - ZSTD_highbit32((U32)STORED_TO_OFFBASE(offcode)) + 1);
  1467. if ((mlRep >= 4) && (gain2 > gain1))
  1468. matchLength = mlRep, offcode = STORE_REPCODE_1, start = ip;
  1469. }
  1470. }
  1471. { size_t offset2=999999999;
  1472. size_t const ml2 = searchMax(ms, ip, iend, &offset2);
  1473. int const gain2 = (int)(ml2*4 - ZSTD_highbit32((U32)STORED_TO_OFFBASE(offset2))); /* raw approx */
  1474. int const gain1 = (int)(matchLength*4 - ZSTD_highbit32((U32)STORED_TO_OFFBASE(offcode)) + 7);
  1475. if ((ml2 >= 4) && (gain2 > gain1)) {
  1476. matchLength = ml2, offcode = offset2, start = ip;
  1477. continue;
  1478. } } }
  1479. break; /* nothing found : store previous solution */
  1480. }
  1481. /* NOTE:
  1482. * Pay attention that `start[-value]` can lead to strange undefined behavior
  1483. * notably if `value` is unsigned, resulting in a large positive `-value`.
  1484. */
  1485. /* catch up */
  1486. if (STORED_IS_OFFSET(offcode)) {
  1487. if (dictMode == ZSTD_noDict) {
  1488. while ( ((start > anchor) & (start - STORED_OFFSET(offcode) > prefixLowest))
  1489. && (start[-1] == (start-STORED_OFFSET(offcode))[-1]) ) /* only search for offset within prefix */
  1490. { start--; matchLength++; }
  1491. }
  1492. if (isDxS) {
  1493. U32 const matchIndex = (U32)((size_t)(start-base) - STORED_OFFSET(offcode));
  1494. const BYTE* match = (matchIndex < prefixLowestIndex) ? dictBase + matchIndex - dictIndexDelta : base + matchIndex;
  1495. const BYTE* const mStart = (matchIndex < prefixLowestIndex) ? dictLowest : prefixLowest;
  1496. while ((start>anchor) && (match>mStart) && (start[-1] == match[-1])) { start--; match--; matchLength++; } /* catch up */
  1497. }
  1498. offset_2 = offset_1; offset_1 = (U32)STORED_OFFSET(offcode);
  1499. }
  1500. /* store sequence */
  1501. _storeSequence:
  1502. { size_t const litLength = (size_t)(start - anchor);
  1503. ZSTD_storeSeq(seqStore, litLength, anchor, iend, (U32)offcode, matchLength);
  1504. anchor = ip = start + matchLength;
  1505. }
  1506. /* check immediate repcode */
  1507. if (isDxS) {
  1508. while (ip <= ilimit) {
  1509. U32 const current2 = (U32)(ip-base);
  1510. U32 const repIndex = current2 - offset_2;
  1511. const BYTE* repMatch = repIndex < prefixLowestIndex ?
  1512. dictBase - dictIndexDelta + repIndex :
  1513. base + repIndex;
  1514. if ( ((U32)((prefixLowestIndex-1) - (U32)repIndex) >= 3 /* intentional overflow */)
  1515. && (MEM_read32(repMatch) == MEM_read32(ip)) ) {
  1516. const BYTE* const repEnd2 = repIndex < prefixLowestIndex ? dictEnd : iend;
  1517. matchLength = ZSTD_count_2segments(ip+4, repMatch+4, iend, repEnd2, prefixLowest) + 4;
  1518. offcode = offset_2; offset_2 = offset_1; offset_1 = (U32)offcode; /* swap offset_2 <=> offset_1 */
  1519. ZSTD_storeSeq(seqStore, 0, anchor, iend, STORE_REPCODE_1, matchLength);
  1520. ip += matchLength;
  1521. anchor = ip;
  1522. continue;
  1523. }
  1524. break;
  1525. }
  1526. }
  1527. if (dictMode == ZSTD_noDict) {
  1528. while ( ((ip <= ilimit) & (offset_2>0))
  1529. && (MEM_read32(ip) == MEM_read32(ip - offset_2)) ) {
  1530. /* store sequence */
  1531. matchLength = ZSTD_count(ip+4, ip+4-offset_2, iend) + 4;
  1532. offcode = offset_2; offset_2 = offset_1; offset_1 = (U32)offcode; /* swap repcodes */
  1533. ZSTD_storeSeq(seqStore, 0, anchor, iend, STORE_REPCODE_1, matchLength);
  1534. ip += matchLength;
  1535. anchor = ip;
  1536. continue; /* faster when present ... (?) */
  1537. } } }
  1538. /* Save reps for next block */
  1539. rep[0] = offset_1 ? offset_1 : savedOffset;
  1540. rep[1] = offset_2 ? offset_2 : savedOffset;
  1541. /* Return the last literals size */
  1542. return (size_t)(iend - anchor);
  1543. }
  1544. size_t ZSTD_compressBlock_btlazy2(
  1545. ZSTD_matchState_t* ms, seqStore_t* seqStore, U32 rep[ZSTD_REP_NUM],
  1546. void const* src, size_t srcSize)
  1547. {
  1548. return ZSTD_compressBlock_lazy_generic(ms, seqStore, rep, src, srcSize, search_binaryTree, 2, ZSTD_noDict);
  1549. }
  1550. size_t ZSTD_compressBlock_lazy2(
  1551. ZSTD_matchState_t* ms, seqStore_t* seqStore, U32 rep[ZSTD_REP_NUM],
  1552. void const* src, size_t srcSize)
  1553. {
  1554. return ZSTD_compressBlock_lazy_generic(ms, seqStore, rep, src, srcSize, search_hashChain, 2, ZSTD_noDict);
  1555. }
  1556. size_t ZSTD_compressBlock_lazy(
  1557. ZSTD_matchState_t* ms, seqStore_t* seqStore, U32 rep[ZSTD_REP_NUM],
  1558. void const* src, size_t srcSize)
  1559. {
  1560. return ZSTD_compressBlock_lazy_generic(ms, seqStore, rep, src, srcSize, search_hashChain, 1, ZSTD_noDict);
  1561. }
  1562. size_t ZSTD_compressBlock_greedy(
  1563. ZSTD_matchState_t* ms, seqStore_t* seqStore, U32 rep[ZSTD_REP_NUM],
  1564. void const* src, size_t srcSize)
  1565. {
  1566. return ZSTD_compressBlock_lazy_generic(ms, seqStore, rep, src, srcSize, search_hashChain, 0, ZSTD_noDict);
  1567. }
  1568. size_t ZSTD_compressBlock_btlazy2_dictMatchState(
  1569. ZSTD_matchState_t* ms, seqStore_t* seqStore, U32 rep[ZSTD_REP_NUM],
  1570. void const* src, size_t srcSize)
  1571. {
  1572. return ZSTD_compressBlock_lazy_generic(ms, seqStore, rep, src, srcSize, search_binaryTree, 2, ZSTD_dictMatchState);
  1573. }
  1574. size_t ZSTD_compressBlock_lazy2_dictMatchState(
  1575. ZSTD_matchState_t* ms, seqStore_t* seqStore, U32 rep[ZSTD_REP_NUM],
  1576. void const* src, size_t srcSize)
  1577. {
  1578. return ZSTD_compressBlock_lazy_generic(ms, seqStore, rep, src, srcSize, search_hashChain, 2, ZSTD_dictMatchState);
  1579. }
  1580. size_t ZSTD_compressBlock_lazy_dictMatchState(
  1581. ZSTD_matchState_t* ms, seqStore_t* seqStore, U32 rep[ZSTD_REP_NUM],
  1582. void const* src, size_t srcSize)
  1583. {
  1584. return ZSTD_compressBlock_lazy_generic(ms, seqStore, rep, src, srcSize, search_hashChain, 1, ZSTD_dictMatchState);
  1585. }
  1586. size_t ZSTD_compressBlock_greedy_dictMatchState(
  1587. ZSTD_matchState_t* ms, seqStore_t* seqStore, U32 rep[ZSTD_REP_NUM],
  1588. void const* src, size_t srcSize)
  1589. {
  1590. return ZSTD_compressBlock_lazy_generic(ms, seqStore, rep, src, srcSize, search_hashChain, 0, ZSTD_dictMatchState);
  1591. }
  1592. size_t ZSTD_compressBlock_lazy2_dedicatedDictSearch(
  1593. ZSTD_matchState_t* ms, seqStore_t* seqStore, U32 rep[ZSTD_REP_NUM],
  1594. void const* src, size_t srcSize)
  1595. {
  1596. return ZSTD_compressBlock_lazy_generic(ms, seqStore, rep, src, srcSize, search_hashChain, 2, ZSTD_dedicatedDictSearch);
  1597. }
  1598. size_t ZSTD_compressBlock_lazy_dedicatedDictSearch(
  1599. ZSTD_matchState_t* ms, seqStore_t* seqStore, U32 rep[ZSTD_REP_NUM],
  1600. void const* src, size_t srcSize)
  1601. {
  1602. return ZSTD_compressBlock_lazy_generic(ms, seqStore, rep, src, srcSize, search_hashChain, 1, ZSTD_dedicatedDictSearch);
  1603. }
  1604. size_t ZSTD_compressBlock_greedy_dedicatedDictSearch(
  1605. ZSTD_matchState_t* ms, seqStore_t* seqStore, U32 rep[ZSTD_REP_NUM],
  1606. void const* src, size_t srcSize)
  1607. {
  1608. return ZSTD_compressBlock_lazy_generic(ms, seqStore, rep, src, srcSize, search_hashChain, 0, ZSTD_dedicatedDictSearch);
  1609. }
  1610. /* Row-based matchfinder */
  1611. size_t ZSTD_compressBlock_lazy2_row(
  1612. ZSTD_matchState_t* ms, seqStore_t* seqStore, U32 rep[ZSTD_REP_NUM],
  1613. void const* src, size_t srcSize)
  1614. {
  1615. return ZSTD_compressBlock_lazy_generic(ms, seqStore, rep, src, srcSize, search_rowHash, 2, ZSTD_noDict);
  1616. }
  1617. size_t ZSTD_compressBlock_lazy_row(
  1618. ZSTD_matchState_t* ms, seqStore_t* seqStore, U32 rep[ZSTD_REP_NUM],
  1619. void const* src, size_t srcSize)
  1620. {
  1621. return ZSTD_compressBlock_lazy_generic(ms, seqStore, rep, src, srcSize, search_rowHash, 1, ZSTD_noDict);
  1622. }
  1623. size_t ZSTD_compressBlock_greedy_row(
  1624. ZSTD_matchState_t* ms, seqStore_t* seqStore, U32 rep[ZSTD_REP_NUM],
  1625. void const* src, size_t srcSize)
  1626. {
  1627. return ZSTD_compressBlock_lazy_generic(ms, seqStore, rep, src, srcSize, search_rowHash, 0, ZSTD_noDict);
  1628. }
  1629. size_t ZSTD_compressBlock_lazy2_dictMatchState_row(
  1630. ZSTD_matchState_t* ms, seqStore_t* seqStore, U32 rep[ZSTD_REP_NUM],
  1631. void const* src, size_t srcSize)
  1632. {
  1633. return ZSTD_compressBlock_lazy_generic(ms, seqStore, rep, src, srcSize, search_rowHash, 2, ZSTD_dictMatchState);
  1634. }
  1635. size_t ZSTD_compressBlock_lazy_dictMatchState_row(
  1636. ZSTD_matchState_t* ms, seqStore_t* seqStore, U32 rep[ZSTD_REP_NUM],
  1637. void const* src, size_t srcSize)
  1638. {
  1639. return ZSTD_compressBlock_lazy_generic(ms, seqStore, rep, src, srcSize, search_rowHash, 1, ZSTD_dictMatchState);
  1640. }
  1641. size_t ZSTD_compressBlock_greedy_dictMatchState_row(
  1642. ZSTD_matchState_t* ms, seqStore_t* seqStore, U32 rep[ZSTD_REP_NUM],
  1643. void const* src, size_t srcSize)
  1644. {
  1645. return ZSTD_compressBlock_lazy_generic(ms, seqStore, rep, src, srcSize, search_rowHash, 0, ZSTD_dictMatchState);
  1646. }
  1647. size_t ZSTD_compressBlock_lazy2_dedicatedDictSearch_row(
  1648. ZSTD_matchState_t* ms, seqStore_t* seqStore, U32 rep[ZSTD_REP_NUM],
  1649. void const* src, size_t srcSize)
  1650. {
  1651. return ZSTD_compressBlock_lazy_generic(ms, seqStore, rep, src, srcSize, search_rowHash, 2, ZSTD_dedicatedDictSearch);
  1652. }
  1653. size_t ZSTD_compressBlock_lazy_dedicatedDictSearch_row(
  1654. ZSTD_matchState_t* ms, seqStore_t* seqStore, U32 rep[ZSTD_REP_NUM],
  1655. void const* src, size_t srcSize)
  1656. {
  1657. return ZSTD_compressBlock_lazy_generic(ms, seqStore, rep, src, srcSize, search_rowHash, 1, ZSTD_dedicatedDictSearch);
  1658. }
  1659. size_t ZSTD_compressBlock_greedy_dedicatedDictSearch_row(
  1660. ZSTD_matchState_t* ms, seqStore_t* seqStore, U32 rep[ZSTD_REP_NUM],
  1661. void const* src, size_t srcSize)
  1662. {
  1663. return ZSTD_compressBlock_lazy_generic(ms, seqStore, rep, src, srcSize, search_rowHash, 0, ZSTD_dedicatedDictSearch);
  1664. }
  1665. FORCE_INLINE_TEMPLATE
  1666. size_t ZSTD_compressBlock_lazy_extDict_generic(
  1667. ZSTD_matchState_t* ms, seqStore_t* seqStore,
  1668. U32 rep[ZSTD_REP_NUM],
  1669. const void* src, size_t srcSize,
  1670. const searchMethod_e searchMethod, const U32 depth)
  1671. {
  1672. const BYTE* const istart = (const BYTE*)src;
  1673. const BYTE* ip = istart;
  1674. const BYTE* anchor = istart;
  1675. const BYTE* const iend = istart + srcSize;
  1676. const BYTE* const ilimit = searchMethod == search_rowHash ? iend - 8 - ZSTD_ROW_HASH_CACHE_SIZE : iend - 8;
  1677. const BYTE* const base = ms->window.base;
  1678. const U32 dictLimit = ms->window.dictLimit;
  1679. const BYTE* const prefixStart = base + dictLimit;
  1680. const BYTE* const dictBase = ms->window.dictBase;
  1681. const BYTE* const dictEnd = dictBase + dictLimit;
  1682. const BYTE* const dictStart = dictBase + ms->window.lowLimit;
  1683. const U32 windowLog = ms->cParams.windowLog;
  1684. const U32 rowLog = ms->cParams.searchLog < 5 ? 4 : 5;
  1685. searchMax_f const searchMax = ZSTD_selectLazyVTable(ms, searchMethod, ZSTD_extDict)->searchMax;
  1686. U32 offset_1 = rep[0], offset_2 = rep[1];
  1687. DEBUGLOG(5, "ZSTD_compressBlock_lazy_extDict_generic (searchFunc=%u)", (U32)searchMethod);
  1688. /* init */
  1689. ip += (ip == prefixStart);
  1690. if (searchMethod == search_rowHash) {
  1691. ZSTD_row_fillHashCache(ms, base, rowLog,
  1692. MIN(ms->cParams.minMatch, 6 /* mls caps out at 6 */),
  1693. ms->nextToUpdate, ilimit);
  1694. }
  1695. /* Match Loop */
  1696. #if defined(__GNUC__) && defined(__x86_64__)
  1697. /* I've measured random a 5% speed loss on levels 5 & 6 (greedy) when the
  1698. * code alignment is perturbed. To fix the instability align the loop on 32-bytes.
  1699. */
  1700. __asm__(".p2align 5");
  1701. #endif
  1702. while (ip < ilimit) {
  1703. size_t matchLength=0;
  1704. size_t offcode=STORE_REPCODE_1;
  1705. const BYTE* start=ip+1;
  1706. U32 curr = (U32)(ip-base);
  1707. /* check repCode */
  1708. { const U32 windowLow = ZSTD_getLowestMatchIndex(ms, curr+1, windowLog);
  1709. const U32 repIndex = (U32)(curr+1 - offset_1);
  1710. const BYTE* const repBase = repIndex < dictLimit ? dictBase : base;
  1711. const BYTE* const repMatch = repBase + repIndex;
  1712. if ( ((U32)((dictLimit-1) - repIndex) >= 3) /* intentional overflow */
  1713. & (offset_1 <= curr+1 - windowLow) ) /* note: we are searching at curr+1 */
  1714. if (MEM_read32(ip+1) == MEM_read32(repMatch)) {
  1715. /* repcode detected we should take it */
  1716. const BYTE* const repEnd = repIndex < dictLimit ? dictEnd : iend;
  1717. matchLength = ZSTD_count_2segments(ip+1+4, repMatch+4, iend, repEnd, prefixStart) + 4;
  1718. if (depth==0) goto _storeSequence;
  1719. } }
  1720. /* first search (depth 0) */
  1721. { size_t offsetFound = 999999999;
  1722. size_t const ml2 = searchMax(ms, ip, iend, &offsetFound);
  1723. if (ml2 > matchLength)
  1724. matchLength = ml2, start = ip, offcode=offsetFound;
  1725. }
  1726. if (matchLength < 4) {
  1727. ip += ((ip-anchor) >> kSearchStrength) + 1; /* jump faster over incompressible sections */
  1728. continue;
  1729. }
  1730. /* let's try to find a better solution */
  1731. if (depth>=1)
  1732. while (ip<ilimit) {
  1733. ip ++;
  1734. curr++;
  1735. /* check repCode */
  1736. if (offcode) {
  1737. const U32 windowLow = ZSTD_getLowestMatchIndex(ms, curr, windowLog);
  1738. const U32 repIndex = (U32)(curr - offset_1);
  1739. const BYTE* const repBase = repIndex < dictLimit ? dictBase : base;
  1740. const BYTE* const repMatch = repBase + repIndex;
  1741. if ( ((U32)((dictLimit-1) - repIndex) >= 3) /* intentional overflow : do not test positions overlapping 2 memory segments */
  1742. & (offset_1 <= curr - windowLow) ) /* equivalent to `curr > repIndex >= windowLow` */
  1743. if (MEM_read32(ip) == MEM_read32(repMatch)) {
  1744. /* repcode detected */
  1745. const BYTE* const repEnd = repIndex < dictLimit ? dictEnd : iend;
  1746. size_t const repLength = ZSTD_count_2segments(ip+4, repMatch+4, iend, repEnd, prefixStart) + 4;
  1747. int const gain2 = (int)(repLength * 3);
  1748. int const gain1 = (int)(matchLength*3 - ZSTD_highbit32((U32)STORED_TO_OFFBASE(offcode)) + 1);
  1749. if ((repLength >= 4) && (gain2 > gain1))
  1750. matchLength = repLength, offcode = STORE_REPCODE_1, start = ip;
  1751. } }
  1752. /* search match, depth 1 */
  1753. { size_t offset2=999999999;
  1754. size_t const ml2 = searchMax(ms, ip, iend, &offset2);
  1755. int const gain2 = (int)(ml2*4 - ZSTD_highbit32((U32)STORED_TO_OFFBASE(offset2))); /* raw approx */
  1756. int const gain1 = (int)(matchLength*4 - ZSTD_highbit32((U32)STORED_TO_OFFBASE(offcode)) + 4);
  1757. if ((ml2 >= 4) && (gain2 > gain1)) {
  1758. matchLength = ml2, offcode = offset2, start = ip;
  1759. continue; /* search a better one */
  1760. } }
  1761. /* let's find an even better one */
  1762. if ((depth==2) && (ip<ilimit)) {
  1763. ip ++;
  1764. curr++;
  1765. /* check repCode */
  1766. if (offcode) {
  1767. const U32 windowLow = ZSTD_getLowestMatchIndex(ms, curr, windowLog);
  1768. const U32 repIndex = (U32)(curr - offset_1);
  1769. const BYTE* const repBase = repIndex < dictLimit ? dictBase : base;
  1770. const BYTE* const repMatch = repBase + repIndex;
  1771. if ( ((U32)((dictLimit-1) - repIndex) >= 3) /* intentional overflow : do not test positions overlapping 2 memory segments */
  1772. & (offset_1 <= curr - windowLow) ) /* equivalent to `curr > repIndex >= windowLow` */
  1773. if (MEM_read32(ip) == MEM_read32(repMatch)) {
  1774. /* repcode detected */
  1775. const BYTE* const repEnd = repIndex < dictLimit ? dictEnd : iend;
  1776. size_t const repLength = ZSTD_count_2segments(ip+4, repMatch+4, iend, repEnd, prefixStart) + 4;
  1777. int const gain2 = (int)(repLength * 4);
  1778. int const gain1 = (int)(matchLength*4 - ZSTD_highbit32((U32)STORED_TO_OFFBASE(offcode)) + 1);
  1779. if ((repLength >= 4) && (gain2 > gain1))
  1780. matchLength = repLength, offcode = STORE_REPCODE_1, start = ip;
  1781. } }
  1782. /* search match, depth 2 */
  1783. { size_t offset2=999999999;
  1784. size_t const ml2 = searchMax(ms, ip, iend, &offset2);
  1785. int const gain2 = (int)(ml2*4 - ZSTD_highbit32((U32)STORED_TO_OFFBASE(offset2))); /* raw approx */
  1786. int const gain1 = (int)(matchLength*4 - ZSTD_highbit32((U32)STORED_TO_OFFBASE(offcode)) + 7);
  1787. if ((ml2 >= 4) && (gain2 > gain1)) {
  1788. matchLength = ml2, offcode = offset2, start = ip;
  1789. continue;
  1790. } } }
  1791. break; /* nothing found : store previous solution */
  1792. }
  1793. /* catch up */
  1794. if (STORED_IS_OFFSET(offcode)) {
  1795. U32 const matchIndex = (U32)((size_t)(start-base) - STORED_OFFSET(offcode));
  1796. const BYTE* match = (matchIndex < dictLimit) ? dictBase + matchIndex : base + matchIndex;
  1797. const BYTE* const mStart = (matchIndex < dictLimit) ? dictStart : prefixStart;
  1798. while ((start>anchor) && (match>mStart) && (start[-1] == match[-1])) { start--; match--; matchLength++; } /* catch up */
  1799. offset_2 = offset_1; offset_1 = (U32)STORED_OFFSET(offcode);
  1800. }
  1801. /* store sequence */
  1802. _storeSequence:
  1803. { size_t const litLength = (size_t)(start - anchor);
  1804. ZSTD_storeSeq(seqStore, litLength, anchor, iend, (U32)offcode, matchLength);
  1805. anchor = ip = start + matchLength;
  1806. }
  1807. /* check immediate repcode */
  1808. while (ip <= ilimit) {
  1809. const U32 repCurrent = (U32)(ip-base);
  1810. const U32 windowLow = ZSTD_getLowestMatchIndex(ms, repCurrent, windowLog);
  1811. const U32 repIndex = repCurrent - offset_2;
  1812. const BYTE* const repBase = repIndex < dictLimit ? dictBase : base;
  1813. const BYTE* const repMatch = repBase + repIndex;
  1814. if ( ((U32)((dictLimit-1) - repIndex) >= 3) /* intentional overflow : do not test positions overlapping 2 memory segments */
  1815. & (offset_2 <= repCurrent - windowLow) ) /* equivalent to `curr > repIndex >= windowLow` */
  1816. if (MEM_read32(ip) == MEM_read32(repMatch)) {
  1817. /* repcode detected we should take it */
  1818. const BYTE* const repEnd = repIndex < dictLimit ? dictEnd : iend;
  1819. matchLength = ZSTD_count_2segments(ip+4, repMatch+4, iend, repEnd, prefixStart) + 4;
  1820. offcode = offset_2; offset_2 = offset_1; offset_1 = (U32)offcode; /* swap offset history */
  1821. ZSTD_storeSeq(seqStore, 0, anchor, iend, STORE_REPCODE_1, matchLength);
  1822. ip += matchLength;
  1823. anchor = ip;
  1824. continue; /* faster when present ... (?) */
  1825. }
  1826. break;
  1827. } }
  1828. /* Save reps for next block */
  1829. rep[0] = offset_1;
  1830. rep[1] = offset_2;
  1831. /* Return the last literals size */
  1832. return (size_t)(iend - anchor);
  1833. }
  1834. size_t ZSTD_compressBlock_greedy_extDict(
  1835. ZSTD_matchState_t* ms, seqStore_t* seqStore, U32 rep[ZSTD_REP_NUM],
  1836. void const* src, size_t srcSize)
  1837. {
  1838. return ZSTD_compressBlock_lazy_extDict_generic(ms, seqStore, rep, src, srcSize, search_hashChain, 0);
  1839. }
  1840. size_t ZSTD_compressBlock_lazy_extDict(
  1841. ZSTD_matchState_t* ms, seqStore_t* seqStore, U32 rep[ZSTD_REP_NUM],
  1842. void const* src, size_t srcSize)
  1843. {
  1844. return ZSTD_compressBlock_lazy_extDict_generic(ms, seqStore, rep, src, srcSize, search_hashChain, 1);
  1845. }
  1846. size_t ZSTD_compressBlock_lazy2_extDict(
  1847. ZSTD_matchState_t* ms, seqStore_t* seqStore, U32 rep[ZSTD_REP_NUM],
  1848. void const* src, size_t srcSize)
  1849. {
  1850. return ZSTD_compressBlock_lazy_extDict_generic(ms, seqStore, rep, src, srcSize, search_hashChain, 2);
  1851. }
  1852. size_t ZSTD_compressBlock_btlazy2_extDict(
  1853. ZSTD_matchState_t* ms, seqStore_t* seqStore, U32 rep[ZSTD_REP_NUM],
  1854. void const* src, size_t srcSize)
  1855. {
  1856. return ZSTD_compressBlock_lazy_extDict_generic(ms, seqStore, rep, src, srcSize, search_binaryTree, 2);
  1857. }
  1858. size_t ZSTD_compressBlock_greedy_extDict_row(
  1859. ZSTD_matchState_t* ms, seqStore_t* seqStore, U32 rep[ZSTD_REP_NUM],
  1860. void const* src, size_t srcSize)
  1861. {
  1862. return ZSTD_compressBlock_lazy_extDict_generic(ms, seqStore, rep, src, srcSize, search_rowHash, 0);
  1863. }
  1864. size_t ZSTD_compressBlock_lazy_extDict_row(
  1865. ZSTD_matchState_t* ms, seqStore_t* seqStore, U32 rep[ZSTD_REP_NUM],
  1866. void const* src, size_t srcSize)
  1867. {
  1868. return ZSTD_compressBlock_lazy_extDict_generic(ms, seqStore, rep, src, srcSize, search_rowHash, 1);
  1869. }
  1870. size_t ZSTD_compressBlock_lazy2_extDict_row(
  1871. ZSTD_matchState_t* ms, seqStore_t* seqStore, U32 rep[ZSTD_REP_NUM],
  1872. void const* src, size_t srcSize)
  1873. {
  1874. return ZSTD_compressBlock_lazy_extDict_generic(ms, seqStore, rep, src, srcSize, search_rowHash, 2);
  1875. }