You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

zstd_lazy.c 98KB

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