zstd_double_fast.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778
  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_double_fast.h"
  12. #ifndef ZSTD_EXCLUDE_DFAST_BLOCK_COMPRESSOR
  13. static
  14. ZSTD_ALLOW_POINTER_OVERFLOW_ATTR
  15. void ZSTD_fillDoubleHashTableForCDict(ZSTD_MatchState_t* ms,
  16. void const* end, ZSTD_dictTableLoadMethod_e dtlm)
  17. {
  18. const ZSTD_compressionParameters* const cParams = &ms->cParams;
  19. U32* const hashLarge = ms->hashTable;
  20. U32 const hBitsL = cParams->hashLog + ZSTD_SHORT_CACHE_TAG_BITS;
  21. U32 const mls = cParams->minMatch;
  22. U32* const hashSmall = ms->chainTable;
  23. U32 const hBitsS = cParams->chainLog + ZSTD_SHORT_CACHE_TAG_BITS;
  24. const BYTE* const base = ms->window.base;
  25. const BYTE* ip = base + ms->nextToUpdate;
  26. const BYTE* const iend = ((const BYTE*)end) - HASH_READ_SIZE;
  27. const U32 fastHashFillStep = 3;
  28. /* Always insert every fastHashFillStep position into the hash tables.
  29. * Insert the other positions into the large hash table if their entry
  30. * is empty.
  31. */
  32. for (; ip + fastHashFillStep - 1 <= iend; ip += fastHashFillStep) {
  33. U32 const curr = (U32)(ip - base);
  34. U32 i;
  35. for (i = 0; i < fastHashFillStep; ++i) {
  36. size_t const smHashAndTag = ZSTD_hashPtr(ip + i, hBitsS, mls);
  37. size_t const lgHashAndTag = ZSTD_hashPtr(ip + i, hBitsL, 8);
  38. if (i == 0) {
  39. ZSTD_writeTaggedIndex(hashSmall, smHashAndTag, curr + i);
  40. }
  41. if (i == 0 || hashLarge[lgHashAndTag >> ZSTD_SHORT_CACHE_TAG_BITS] == 0) {
  42. ZSTD_writeTaggedIndex(hashLarge, lgHashAndTag, curr + i);
  43. }
  44. /* Only load extra positions for ZSTD_dtlm_full */
  45. if (dtlm == ZSTD_dtlm_fast)
  46. break;
  47. } }
  48. }
  49. static
  50. ZSTD_ALLOW_POINTER_OVERFLOW_ATTR
  51. void ZSTD_fillDoubleHashTableForCCtx(ZSTD_MatchState_t* ms,
  52. void const* end, ZSTD_dictTableLoadMethod_e dtlm)
  53. {
  54. const ZSTD_compressionParameters* const cParams = &ms->cParams;
  55. U32* const hashLarge = ms->hashTable;
  56. U32 const hBitsL = cParams->hashLog;
  57. U32 const mls = cParams->minMatch;
  58. U32* const hashSmall = ms->chainTable;
  59. U32 const hBitsS = cParams->chainLog;
  60. const BYTE* const base = ms->window.base;
  61. const BYTE* ip = base + ms->nextToUpdate;
  62. const BYTE* const iend = ((const BYTE*)end) - HASH_READ_SIZE;
  63. const U32 fastHashFillStep = 3;
  64. /* Always insert every fastHashFillStep position into the hash tables.
  65. * Insert the other positions into the large hash table if their entry
  66. * is empty.
  67. */
  68. for (; ip + fastHashFillStep - 1 <= iend; ip += fastHashFillStep) {
  69. U32 const curr = (U32)(ip - base);
  70. U32 i;
  71. for (i = 0; i < fastHashFillStep; ++i) {
  72. size_t const smHash = ZSTD_hashPtr(ip + i, hBitsS, mls);
  73. size_t const lgHash = ZSTD_hashPtr(ip + i, hBitsL, 8);
  74. if (i == 0)
  75. hashSmall[smHash] = curr + i;
  76. if (i == 0 || hashLarge[lgHash] == 0)
  77. hashLarge[lgHash] = curr + i;
  78. /* Only load extra positions for ZSTD_dtlm_full */
  79. if (dtlm == ZSTD_dtlm_fast)
  80. break;
  81. } }
  82. }
  83. void ZSTD_fillDoubleHashTable(ZSTD_MatchState_t* ms,
  84. const void* const end,
  85. ZSTD_dictTableLoadMethod_e dtlm,
  86. ZSTD_tableFillPurpose_e tfp)
  87. {
  88. if (tfp == ZSTD_tfp_forCDict) {
  89. ZSTD_fillDoubleHashTableForCDict(ms, end, dtlm);
  90. } else {
  91. ZSTD_fillDoubleHashTableForCCtx(ms, end, dtlm);
  92. }
  93. }
  94. FORCE_INLINE_TEMPLATE
  95. ZSTD_ALLOW_POINTER_OVERFLOW_ATTR
  96. size_t ZSTD_compressBlock_doubleFast_noDict_generic(
  97. ZSTD_MatchState_t* ms, SeqStore_t* seqStore, U32 rep[ZSTD_REP_NUM],
  98. void const* src, size_t srcSize, U32 const mls /* template */)
  99. {
  100. ZSTD_compressionParameters const* cParams = &ms->cParams;
  101. U32* const hashLong = ms->hashTable;
  102. const U32 hBitsL = cParams->hashLog;
  103. U32* const hashSmall = ms->chainTable;
  104. const U32 hBitsS = cParams->chainLog;
  105. const BYTE* const base = ms->window.base;
  106. const BYTE* const istart = (const BYTE*)src;
  107. const BYTE* anchor = istart;
  108. const U32 endIndex = (U32)((size_t)(istart - base) + srcSize);
  109. /* presumes that, if there is a dictionary, it must be using Attach mode */
  110. const U32 prefixLowestIndex = ZSTD_getLowestPrefixIndex(ms, endIndex, cParams->windowLog);
  111. const BYTE* const prefixLowest = base + prefixLowestIndex;
  112. const BYTE* const iend = istart + srcSize;
  113. const BYTE* const ilimit = iend - HASH_READ_SIZE;
  114. U32 offset_1=rep[0], offset_2=rep[1];
  115. U32 offsetSaved1 = 0, offsetSaved2 = 0;
  116. size_t mLength;
  117. U32 offset;
  118. U32 curr;
  119. /* how many positions to search before increasing step size */
  120. const size_t kStepIncr = 1 << kSearchStrength;
  121. /* the position at which to increment the step size if no match is found */
  122. const BYTE* nextStep;
  123. size_t step; /* the current step size */
  124. size_t hl0; /* the long hash at ip */
  125. size_t hl1; /* the long hash at ip1 */
  126. U32 idxl0; /* the long match index for ip */
  127. U32 idxl1; /* the long match index for ip1 */
  128. const BYTE* matchl0; /* the long match for ip */
  129. const BYTE* matchs0; /* the short match for ip */
  130. const BYTE* matchl1; /* the long match for ip1 */
  131. const BYTE* matchs0_safe; /* matchs0 or safe address */
  132. const BYTE* ip = istart; /* the current position */
  133. const BYTE* ip1; /* the next position */
  134. /* Array of ~random data, should have low probability of matching data
  135. * we load from here instead of from tables, if matchl0/matchl1 are
  136. * invalid indices. Used to avoid unpredictable branches. */
  137. const BYTE dummy[] = {0x12,0x34,0x56,0x78,0x9a,0xbc,0xde,0xf0,0xe2,0xb4};
  138. DEBUGLOG(5, "ZSTD_compressBlock_doubleFast_noDict_generic");
  139. /* init */
  140. ip += ((ip - prefixLowest) == 0);
  141. {
  142. U32 const current = (U32)(ip - base);
  143. U32 const windowLow = ZSTD_getLowestPrefixIndex(ms, current, cParams->windowLog);
  144. U32 const maxRep = current - windowLow;
  145. if (offset_2 > maxRep) offsetSaved2 = offset_2, offset_2 = 0;
  146. if (offset_1 > maxRep) offsetSaved1 = offset_1, offset_1 = 0;
  147. }
  148. /* Outer Loop: one iteration per match found and stored */
  149. while (1) {
  150. step = 1;
  151. nextStep = ip + kStepIncr;
  152. ip1 = ip + step;
  153. if (ip1 > ilimit) {
  154. goto _cleanup;
  155. }
  156. hl0 = ZSTD_hashPtr(ip, hBitsL, 8);
  157. idxl0 = hashLong[hl0];
  158. matchl0 = base + idxl0;
  159. /* Inner Loop: one iteration per search / position */
  160. do {
  161. const size_t hs0 = ZSTD_hashPtr(ip, hBitsS, mls);
  162. const U32 idxs0 = hashSmall[hs0];
  163. curr = (U32)(ip-base);
  164. matchs0 = base + idxs0;
  165. hashLong[hl0] = hashSmall[hs0] = curr; /* update hash tables */
  166. /* check noDict repcode */
  167. if ((offset_1 > 0) & (MEM_read32(ip+1-offset_1) == MEM_read32(ip+1))) {
  168. mLength = ZSTD_count(ip+1+4, ip+1+4-offset_1, iend) + 4;
  169. ip++;
  170. ZSTD_storeSeq(seqStore, (size_t)(ip-anchor), anchor, iend, REPCODE1_TO_OFFBASE, mLength);
  171. goto _match_stored;
  172. }
  173. hl1 = ZSTD_hashPtr(ip1, hBitsL, 8);
  174. /* idxl0 > prefixLowestIndex is a (somewhat) unpredictable branch.
  175. * However expression below complies into conditional move. Since
  176. * match is unlikely and we only *branch* on idxl0 > prefixLowestIndex
  177. * if there is a match, all branches become predictable. */
  178. { const BYTE* const matchl0_safe = ZSTD_selectAddr(idxl0, prefixLowestIndex, matchl0, &dummy[0]);
  179. /* check prefix long match */
  180. if (MEM_read64(matchl0_safe) == MEM_read64(ip) && matchl0_safe == matchl0) {
  181. mLength = ZSTD_count(ip+8, matchl0+8, iend) + 8;
  182. offset = (U32)(ip-matchl0);
  183. while (((ip>anchor) & (matchl0>prefixLowest)) && (ip[-1] == matchl0[-1])) { ip--; matchl0--; mLength++; } /* catch up */
  184. goto _match_found;
  185. } }
  186. idxl1 = hashLong[hl1];
  187. matchl1 = base + idxl1;
  188. /* Same optimization as matchl0 above */
  189. matchs0_safe = ZSTD_selectAddr(idxs0, prefixLowestIndex, matchs0, &dummy[0]);
  190. /* check prefix short match */
  191. if(MEM_read32(matchs0_safe) == MEM_read32(ip) && matchs0_safe == matchs0) {
  192. goto _search_next_long;
  193. }
  194. if (ip1 >= nextStep) {
  195. PREFETCH_L1(ip1 + 64);
  196. PREFETCH_L1(ip1 + 128);
  197. step++;
  198. nextStep += kStepIncr;
  199. }
  200. ip = ip1;
  201. ip1 += step;
  202. hl0 = hl1;
  203. idxl0 = idxl1;
  204. matchl0 = matchl1;
  205. #if defined(__aarch64__)
  206. PREFETCH_L1(ip+256);
  207. #endif
  208. } while (ip1 <= ilimit);
  209. _cleanup:
  210. /* If offset_1 started invalid (offsetSaved1 != 0) and became valid (offset_1 != 0),
  211. * rotate saved offsets. See comment in ZSTD_compressBlock_fast_noDict for more context. */
  212. offsetSaved2 = ((offsetSaved1 != 0) && (offset_1 != 0)) ? offsetSaved1 : offsetSaved2;
  213. /* save reps for next block */
  214. rep[0] = offset_1 ? offset_1 : offsetSaved1;
  215. rep[1] = offset_2 ? offset_2 : offsetSaved2;
  216. /* Return the last literals size */
  217. return (size_t)(iend - anchor);
  218. _search_next_long:
  219. /* short match found: let's check for a longer one */
  220. mLength = ZSTD_count(ip+4, matchs0+4, iend) + 4;
  221. offset = (U32)(ip - matchs0);
  222. /* check long match at +1 position */
  223. if ((idxl1 > prefixLowestIndex) && (MEM_read64(matchl1) == MEM_read64(ip1))) {
  224. size_t const l1len = ZSTD_count(ip1+8, matchl1+8, iend) + 8;
  225. if (l1len > mLength) {
  226. /* use the long match instead */
  227. ip = ip1;
  228. mLength = l1len;
  229. offset = (U32)(ip-matchl1);
  230. matchs0 = matchl1;
  231. }
  232. }
  233. while (((ip>anchor) & (matchs0>prefixLowest)) && (ip[-1] == matchs0[-1])) { ip--; matchs0--; mLength++; } /* complete backward */
  234. /* fall-through */
  235. _match_found: /* requires ip, offset, mLength */
  236. offset_2 = offset_1;
  237. offset_1 = offset;
  238. if (step < 4) {
  239. /* It is unsafe to write this value back to the hashtable when ip1 is
  240. * greater than or equal to the new ip we will have after we're done
  241. * processing this match. Rather than perform that test directly
  242. * (ip1 >= ip + mLength), which costs speed in practice, we do a simpler
  243. * more predictable test. The minmatch even if we take a short match is
  244. * 4 bytes, so as long as step, the distance between ip and ip1
  245. * (initially) is less than 4, we know ip1 < new ip. */
  246. hashLong[hl1] = (U32)(ip1 - base);
  247. }
  248. ZSTD_storeSeq(seqStore, (size_t)(ip-anchor), anchor, iend, OFFSET_TO_OFFBASE(offset), mLength);
  249. _match_stored:
  250. /* match found */
  251. ip += mLength;
  252. anchor = ip;
  253. if (ip <= ilimit) {
  254. /* Complementary insertion */
  255. /* done after iLimit test, as candidates could be > iend-8 */
  256. { U32 const indexToInsert = curr+2;
  257. hashLong[ZSTD_hashPtr(base+indexToInsert, hBitsL, 8)] = indexToInsert;
  258. hashLong[ZSTD_hashPtr(ip-2, hBitsL, 8)] = (U32)(ip-2-base);
  259. hashSmall[ZSTD_hashPtr(base+indexToInsert, hBitsS, mls)] = indexToInsert;
  260. hashSmall[ZSTD_hashPtr(ip-1, hBitsS, mls)] = (U32)(ip-1-base);
  261. }
  262. /* check immediate repcode */
  263. while ( (ip <= ilimit)
  264. && ( (offset_2>0)
  265. & (MEM_read32(ip) == MEM_read32(ip - offset_2)) )) {
  266. /* store sequence */
  267. size_t const rLength = ZSTD_count(ip+4, ip+4-offset_2, iend) + 4;
  268. U32 const tmpOff = offset_2; offset_2 = offset_1; offset_1 = tmpOff; /* swap offset_2 <=> offset_1 */
  269. hashSmall[ZSTD_hashPtr(ip, hBitsS, mls)] = (U32)(ip-base);
  270. hashLong[ZSTD_hashPtr(ip, hBitsL, 8)] = (U32)(ip-base);
  271. ZSTD_storeSeq(seqStore, 0, anchor, iend, REPCODE1_TO_OFFBASE, rLength);
  272. ip += rLength;
  273. anchor = ip;
  274. continue; /* faster when present ... (?) */
  275. }
  276. }
  277. }
  278. }
  279. FORCE_INLINE_TEMPLATE
  280. ZSTD_ALLOW_POINTER_OVERFLOW_ATTR
  281. size_t ZSTD_compressBlock_doubleFast_dictMatchState_generic(
  282. ZSTD_MatchState_t* ms, SeqStore_t* seqStore, U32 rep[ZSTD_REP_NUM],
  283. void const* src, size_t srcSize,
  284. U32 const mls /* template */)
  285. {
  286. ZSTD_compressionParameters const* cParams = &ms->cParams;
  287. U32* const hashLong = ms->hashTable;
  288. const U32 hBitsL = cParams->hashLog;
  289. U32* const hashSmall = ms->chainTable;
  290. const U32 hBitsS = cParams->chainLog;
  291. const BYTE* const base = ms->window.base;
  292. const BYTE* const istart = (const BYTE*)src;
  293. const BYTE* ip = istart;
  294. const BYTE* anchor = istart;
  295. const U32 endIndex = (U32)((size_t)(istart - base) + srcSize);
  296. /* presumes that, if there is a dictionary, it must be using Attach mode */
  297. const U32 prefixLowestIndex = ZSTD_getLowestPrefixIndex(ms, endIndex, cParams->windowLog);
  298. const BYTE* const prefixLowest = base + prefixLowestIndex;
  299. const BYTE* const iend = istart + srcSize;
  300. const BYTE* const ilimit = iend - HASH_READ_SIZE;
  301. U32 offset_1=rep[0], offset_2=rep[1];
  302. const ZSTD_MatchState_t* const dms = ms->dictMatchState;
  303. const ZSTD_compressionParameters* const dictCParams = &dms->cParams;
  304. const U32* const dictHashLong = dms->hashTable;
  305. const U32* const dictHashSmall = dms->chainTable;
  306. const U32 dictStartIndex = dms->window.dictLimit;
  307. const BYTE* const dictBase = dms->window.base;
  308. const BYTE* const dictStart = dictBase + dictStartIndex;
  309. const BYTE* const dictEnd = dms->window.nextSrc;
  310. const U32 dictIndexDelta = prefixLowestIndex - (U32)(dictEnd - dictBase);
  311. const U32 dictHBitsL = dictCParams->hashLog + ZSTD_SHORT_CACHE_TAG_BITS;
  312. const U32 dictHBitsS = dictCParams->chainLog + ZSTD_SHORT_CACHE_TAG_BITS;
  313. const U32 dictAndPrefixLength = (U32)((ip - prefixLowest) + (dictEnd - dictStart));
  314. DEBUGLOG(5, "ZSTD_compressBlock_doubleFast_dictMatchState_generic");
  315. /* if a dictionary is attached, it must be within window range */
  316. assert(ms->window.dictLimit + (1U << cParams->windowLog) >= endIndex);
  317. if (ms->prefetchCDictTables) {
  318. size_t const hashTableBytes = (((size_t)1) << dictCParams->hashLog) * sizeof(U32);
  319. size_t const chainTableBytes = (((size_t)1) << dictCParams->chainLog) * sizeof(U32);
  320. PREFETCH_AREA(dictHashLong, hashTableBytes);
  321. PREFETCH_AREA(dictHashSmall, chainTableBytes);
  322. }
  323. /* init */
  324. ip += (dictAndPrefixLength == 0);
  325. /* dictMatchState repCode checks don't currently handle repCode == 0
  326. * disabling. */
  327. assert(offset_1 <= dictAndPrefixLength);
  328. assert(offset_2 <= dictAndPrefixLength);
  329. /* Main Search Loop */
  330. while (ip < ilimit) { /* < instead of <=, because repcode check at (ip+1) */
  331. size_t mLength;
  332. U32 offset;
  333. size_t const h2 = ZSTD_hashPtr(ip, hBitsL, 8);
  334. size_t const h = ZSTD_hashPtr(ip, hBitsS, mls);
  335. size_t const dictHashAndTagL = ZSTD_hashPtr(ip, dictHBitsL, 8);
  336. size_t const dictHashAndTagS = ZSTD_hashPtr(ip, dictHBitsS, mls);
  337. U32 const dictMatchIndexAndTagL = dictHashLong[dictHashAndTagL >> ZSTD_SHORT_CACHE_TAG_BITS];
  338. U32 const dictMatchIndexAndTagS = dictHashSmall[dictHashAndTagS >> ZSTD_SHORT_CACHE_TAG_BITS];
  339. int const dictTagsMatchL = ZSTD_comparePackedTags(dictMatchIndexAndTagL, dictHashAndTagL);
  340. int const dictTagsMatchS = ZSTD_comparePackedTags(dictMatchIndexAndTagS, dictHashAndTagS);
  341. U32 const curr = (U32)(ip-base);
  342. U32 const matchIndexL = hashLong[h2];
  343. U32 matchIndexS = hashSmall[h];
  344. const BYTE* matchLong = base + matchIndexL;
  345. const BYTE* match = base + matchIndexS;
  346. const U32 repIndex = curr + 1 - offset_1;
  347. const BYTE* repMatch = (repIndex < prefixLowestIndex) ?
  348. dictBase + (repIndex - dictIndexDelta) :
  349. base + repIndex;
  350. hashLong[h2] = hashSmall[h] = curr; /* update hash tables */
  351. /* check repcode */
  352. if ((ZSTD_index_overlap_check(prefixLowestIndex, repIndex))
  353. && (MEM_read32(repMatch) == MEM_read32(ip+1)) ) {
  354. const BYTE* repMatchEnd = repIndex < prefixLowestIndex ? dictEnd : iend;
  355. mLength = ZSTD_count_2segments(ip+1+4, repMatch+4, iend, repMatchEnd, prefixLowest) + 4;
  356. ip++;
  357. ZSTD_storeSeq(seqStore, (size_t)(ip-anchor), anchor, iend, REPCODE1_TO_OFFBASE, mLength);
  358. goto _match_stored;
  359. }
  360. if ((matchIndexL >= prefixLowestIndex) && (MEM_read64(matchLong) == MEM_read64(ip))) {
  361. /* check prefix long match */
  362. mLength = ZSTD_count(ip+8, matchLong+8, iend) + 8;
  363. offset = (U32)(ip-matchLong);
  364. while (((ip>anchor) & (matchLong>prefixLowest)) && (ip[-1] == matchLong[-1])) { ip--; matchLong--; mLength++; } /* catch up */
  365. goto _match_found;
  366. } else if (dictTagsMatchL) {
  367. /* check dictMatchState long match */
  368. U32 const dictMatchIndexL = dictMatchIndexAndTagL >> ZSTD_SHORT_CACHE_TAG_BITS;
  369. const BYTE* dictMatchL = dictBase + dictMatchIndexL;
  370. assert(dictMatchL < dictEnd);
  371. if (dictMatchL > dictStart && MEM_read64(dictMatchL) == MEM_read64(ip)) {
  372. mLength = ZSTD_count_2segments(ip+8, dictMatchL+8, iend, dictEnd, prefixLowest) + 8;
  373. offset = (U32)(curr - dictMatchIndexL - dictIndexDelta);
  374. while (((ip>anchor) & (dictMatchL>dictStart)) && (ip[-1] == dictMatchL[-1])) { ip--; dictMatchL--; mLength++; } /* catch up */
  375. goto _match_found;
  376. } }
  377. if (matchIndexS > prefixLowestIndex) {
  378. /* short match candidate */
  379. if (MEM_read32(match) == MEM_read32(ip)) {
  380. goto _search_next_long;
  381. }
  382. } else if (dictTagsMatchS) {
  383. /* check dictMatchState short match */
  384. U32 const dictMatchIndexS = dictMatchIndexAndTagS >> ZSTD_SHORT_CACHE_TAG_BITS;
  385. match = dictBase + dictMatchIndexS;
  386. matchIndexS = dictMatchIndexS + dictIndexDelta;
  387. if (match > dictStart && MEM_read32(match) == MEM_read32(ip)) {
  388. goto _search_next_long;
  389. } }
  390. ip += ((ip-anchor) >> kSearchStrength) + 1;
  391. #if defined(__aarch64__)
  392. PREFETCH_L1(ip+256);
  393. #endif
  394. continue;
  395. _search_next_long:
  396. { size_t const hl3 = ZSTD_hashPtr(ip+1, hBitsL, 8);
  397. size_t const dictHashAndTagL3 = ZSTD_hashPtr(ip+1, dictHBitsL, 8);
  398. U32 const matchIndexL3 = hashLong[hl3];
  399. U32 const dictMatchIndexAndTagL3 = dictHashLong[dictHashAndTagL3 >> ZSTD_SHORT_CACHE_TAG_BITS];
  400. int const dictTagsMatchL3 = ZSTD_comparePackedTags(dictMatchIndexAndTagL3, dictHashAndTagL3);
  401. const BYTE* matchL3 = base + matchIndexL3;
  402. hashLong[hl3] = curr + 1;
  403. /* check prefix long +1 match */
  404. if ((matchIndexL3 >= prefixLowestIndex) && (MEM_read64(matchL3) == MEM_read64(ip+1))) {
  405. mLength = ZSTD_count(ip+9, matchL3+8, iend) + 8;
  406. ip++;
  407. offset = (U32)(ip-matchL3);
  408. while (((ip>anchor) & (matchL3>prefixLowest)) && (ip[-1] == matchL3[-1])) { ip--; matchL3--; mLength++; } /* catch up */
  409. goto _match_found;
  410. } else if (dictTagsMatchL3) {
  411. /* check dict long +1 match */
  412. U32 const dictMatchIndexL3 = dictMatchIndexAndTagL3 >> ZSTD_SHORT_CACHE_TAG_BITS;
  413. const BYTE* dictMatchL3 = dictBase + dictMatchIndexL3;
  414. assert(dictMatchL3 < dictEnd);
  415. if (dictMatchL3 > dictStart && MEM_read64(dictMatchL3) == MEM_read64(ip+1)) {
  416. mLength = ZSTD_count_2segments(ip+1+8, dictMatchL3+8, iend, dictEnd, prefixLowest) + 8;
  417. ip++;
  418. offset = (U32)(curr + 1 - dictMatchIndexL3 - dictIndexDelta);
  419. while (((ip>anchor) & (dictMatchL3>dictStart)) && (ip[-1] == dictMatchL3[-1])) { ip--; dictMatchL3--; mLength++; } /* catch up */
  420. goto _match_found;
  421. } } }
  422. /* if no long +1 match, explore the short match we found */
  423. if (matchIndexS < prefixLowestIndex) {
  424. mLength = ZSTD_count_2segments(ip+4, match+4, iend, dictEnd, prefixLowest) + 4;
  425. offset = (U32)(curr - matchIndexS);
  426. while (((ip>anchor) & (match>dictStart)) && (ip[-1] == match[-1])) { ip--; match--; mLength++; } /* catch up */
  427. } else {
  428. mLength = ZSTD_count(ip+4, match+4, iend) + 4;
  429. offset = (U32)(ip - match);
  430. while (((ip>anchor) & (match>prefixLowest)) && (ip[-1] == match[-1])) { ip--; match--; mLength++; } /* catch up */
  431. }
  432. _match_found:
  433. offset_2 = offset_1;
  434. offset_1 = offset;
  435. ZSTD_storeSeq(seqStore, (size_t)(ip-anchor), anchor, iend, OFFSET_TO_OFFBASE(offset), mLength);
  436. _match_stored:
  437. /* match found */
  438. ip += mLength;
  439. anchor = ip;
  440. if (ip <= ilimit) {
  441. /* Complementary insertion */
  442. /* done after iLimit test, as candidates could be > iend-8 */
  443. { U32 const indexToInsert = curr+2;
  444. hashLong[ZSTD_hashPtr(base+indexToInsert, hBitsL, 8)] = indexToInsert;
  445. hashLong[ZSTD_hashPtr(ip-2, hBitsL, 8)] = (U32)(ip-2-base);
  446. hashSmall[ZSTD_hashPtr(base+indexToInsert, hBitsS, mls)] = indexToInsert;
  447. hashSmall[ZSTD_hashPtr(ip-1, hBitsS, mls)] = (U32)(ip-1-base);
  448. }
  449. /* check immediate repcode */
  450. while (ip <= ilimit) {
  451. U32 const current2 = (U32)(ip-base);
  452. U32 const repIndex2 = current2 - offset_2;
  453. const BYTE* repMatch2 = repIndex2 < prefixLowestIndex ?
  454. dictBase + repIndex2 - dictIndexDelta :
  455. base + repIndex2;
  456. if ( (ZSTD_index_overlap_check(prefixLowestIndex, repIndex2))
  457. && (MEM_read32(repMatch2) == MEM_read32(ip)) ) {
  458. const BYTE* const repEnd2 = repIndex2 < prefixLowestIndex ? dictEnd : iend;
  459. size_t const repLength2 = ZSTD_count_2segments(ip+4, repMatch2+4, iend, repEnd2, prefixLowest) + 4;
  460. U32 tmpOffset = offset_2; offset_2 = offset_1; offset_1 = tmpOffset; /* swap offset_2 <=> offset_1 */
  461. ZSTD_storeSeq(seqStore, 0, anchor, iend, REPCODE1_TO_OFFBASE, repLength2);
  462. hashSmall[ZSTD_hashPtr(ip, hBitsS, mls)] = current2;
  463. hashLong[ZSTD_hashPtr(ip, hBitsL, 8)] = current2;
  464. ip += repLength2;
  465. anchor = ip;
  466. continue;
  467. }
  468. break;
  469. }
  470. }
  471. } /* while (ip < ilimit) */
  472. /* save reps for next block */
  473. rep[0] = offset_1;
  474. rep[1] = offset_2;
  475. /* Return the last literals size */
  476. return (size_t)(iend - anchor);
  477. }
  478. #define ZSTD_GEN_DFAST_FN(dictMode, mls) \
  479. static size_t ZSTD_compressBlock_doubleFast_##dictMode##_##mls( \
  480. ZSTD_MatchState_t* ms, SeqStore_t* seqStore, U32 rep[ZSTD_REP_NUM], \
  481. void const* src, size_t srcSize) \
  482. { \
  483. return ZSTD_compressBlock_doubleFast_##dictMode##_generic(ms, seqStore, rep, src, srcSize, mls); \
  484. }
  485. ZSTD_GEN_DFAST_FN(noDict, 4)
  486. ZSTD_GEN_DFAST_FN(noDict, 5)
  487. ZSTD_GEN_DFAST_FN(noDict, 6)
  488. ZSTD_GEN_DFAST_FN(noDict, 7)
  489. ZSTD_GEN_DFAST_FN(dictMatchState, 4)
  490. ZSTD_GEN_DFAST_FN(dictMatchState, 5)
  491. ZSTD_GEN_DFAST_FN(dictMatchState, 6)
  492. ZSTD_GEN_DFAST_FN(dictMatchState, 7)
  493. size_t ZSTD_compressBlock_doubleFast(
  494. ZSTD_MatchState_t* ms, SeqStore_t* seqStore, U32 rep[ZSTD_REP_NUM],
  495. void const* src, size_t srcSize)
  496. {
  497. const U32 mls = ms->cParams.minMatch;
  498. switch(mls)
  499. {
  500. default: /* includes case 3 */
  501. case 4 :
  502. return ZSTD_compressBlock_doubleFast_noDict_4(ms, seqStore, rep, src, srcSize);
  503. case 5 :
  504. return ZSTD_compressBlock_doubleFast_noDict_5(ms, seqStore, rep, src, srcSize);
  505. case 6 :
  506. return ZSTD_compressBlock_doubleFast_noDict_6(ms, seqStore, rep, src, srcSize);
  507. case 7 :
  508. return ZSTD_compressBlock_doubleFast_noDict_7(ms, seqStore, rep, src, srcSize);
  509. }
  510. }
  511. size_t ZSTD_compressBlock_doubleFast_dictMatchState(
  512. ZSTD_MatchState_t* ms, SeqStore_t* seqStore, U32 rep[ZSTD_REP_NUM],
  513. void const* src, size_t srcSize)
  514. {
  515. const U32 mls = ms->cParams.minMatch;
  516. switch(mls)
  517. {
  518. default: /* includes case 3 */
  519. case 4 :
  520. return ZSTD_compressBlock_doubleFast_dictMatchState_4(ms, seqStore, rep, src, srcSize);
  521. case 5 :
  522. return ZSTD_compressBlock_doubleFast_dictMatchState_5(ms, seqStore, rep, src, srcSize);
  523. case 6 :
  524. return ZSTD_compressBlock_doubleFast_dictMatchState_6(ms, seqStore, rep, src, srcSize);
  525. case 7 :
  526. return ZSTD_compressBlock_doubleFast_dictMatchState_7(ms, seqStore, rep, src, srcSize);
  527. }
  528. }
  529. static
  530. ZSTD_ALLOW_POINTER_OVERFLOW_ATTR
  531. size_t ZSTD_compressBlock_doubleFast_extDict_generic(
  532. ZSTD_MatchState_t* ms, SeqStore_t* seqStore, U32 rep[ZSTD_REP_NUM],
  533. void const* src, size_t srcSize,
  534. U32 const mls /* template */)
  535. {
  536. ZSTD_compressionParameters const* cParams = &ms->cParams;
  537. U32* const hashLong = ms->hashTable;
  538. U32 const hBitsL = cParams->hashLog;
  539. U32* const hashSmall = ms->chainTable;
  540. U32 const hBitsS = cParams->chainLog;
  541. const BYTE* const istart = (const BYTE*)src;
  542. const BYTE* ip = istart;
  543. const BYTE* anchor = istart;
  544. const BYTE* const iend = istart + srcSize;
  545. const BYTE* const ilimit = iend - 8;
  546. const BYTE* const base = ms->window.base;
  547. const U32 endIndex = (U32)((size_t)(istart - base) + srcSize);
  548. const U32 lowLimit = ZSTD_getLowestMatchIndex(ms, endIndex, cParams->windowLog);
  549. const U32 dictStartIndex = lowLimit;
  550. const U32 dictLimit = ms->window.dictLimit;
  551. const U32 prefixStartIndex = (dictLimit > lowLimit) ? dictLimit : lowLimit;
  552. const BYTE* const prefixStart = base + prefixStartIndex;
  553. const BYTE* const dictBase = ms->window.dictBase;
  554. const BYTE* const dictStart = dictBase + dictStartIndex;
  555. const BYTE* const dictEnd = dictBase + prefixStartIndex;
  556. U32 offset_1=rep[0], offset_2=rep[1];
  557. DEBUGLOG(5, "ZSTD_compressBlock_doubleFast_extDict_generic (srcSize=%zu)", srcSize);
  558. /* if extDict is invalidated due to maxDistance, switch to "regular" variant */
  559. if (prefixStartIndex == dictStartIndex)
  560. return ZSTD_compressBlock_doubleFast(ms, seqStore, rep, src, srcSize);
  561. /* Search Loop */
  562. while (ip < ilimit) { /* < instead of <=, because (ip+1) */
  563. const size_t hSmall = ZSTD_hashPtr(ip, hBitsS, mls);
  564. const U32 matchIndex = hashSmall[hSmall];
  565. const BYTE* const matchBase = matchIndex < prefixStartIndex ? dictBase : base;
  566. const BYTE* match = matchBase + matchIndex;
  567. const size_t hLong = ZSTD_hashPtr(ip, hBitsL, 8);
  568. const U32 matchLongIndex = hashLong[hLong];
  569. const BYTE* const matchLongBase = matchLongIndex < prefixStartIndex ? dictBase : base;
  570. const BYTE* matchLong = matchLongBase + matchLongIndex;
  571. const U32 curr = (U32)(ip-base);
  572. const U32 repIndex = curr + 1 - offset_1; /* offset_1 expected <= curr +1 */
  573. const BYTE* const repBase = repIndex < prefixStartIndex ? dictBase : base;
  574. const BYTE* const repMatch = repBase + repIndex;
  575. size_t mLength;
  576. hashSmall[hSmall] = hashLong[hLong] = curr; /* update hash table */
  577. if (((ZSTD_index_overlap_check(prefixStartIndex, repIndex))
  578. & (offset_1 <= curr+1 - dictStartIndex)) /* note: we are searching at curr+1 */
  579. && (MEM_read32(repMatch) == MEM_read32(ip+1)) ) {
  580. const BYTE* repMatchEnd = repIndex < prefixStartIndex ? dictEnd : iend;
  581. mLength = ZSTD_count_2segments(ip+1+4, repMatch+4, iend, repMatchEnd, prefixStart) + 4;
  582. ip++;
  583. ZSTD_storeSeq(seqStore, (size_t)(ip-anchor), anchor, iend, REPCODE1_TO_OFFBASE, mLength);
  584. } else {
  585. if ((matchLongIndex > dictStartIndex) && (MEM_read64(matchLong) == MEM_read64(ip))) {
  586. const BYTE* const matchEnd = matchLongIndex < prefixStartIndex ? dictEnd : iend;
  587. const BYTE* const lowMatchPtr = matchLongIndex < prefixStartIndex ? dictStart : prefixStart;
  588. U32 offset;
  589. mLength = ZSTD_count_2segments(ip+8, matchLong+8, iend, matchEnd, prefixStart) + 8;
  590. offset = curr - matchLongIndex;
  591. while (((ip>anchor) & (matchLong>lowMatchPtr)) && (ip[-1] == matchLong[-1])) { ip--; matchLong--; mLength++; } /* catch up */
  592. offset_2 = offset_1;
  593. offset_1 = offset;
  594. ZSTD_storeSeq(seqStore, (size_t)(ip-anchor), anchor, iend, OFFSET_TO_OFFBASE(offset), mLength);
  595. } else if ((matchIndex > dictStartIndex) && (MEM_read32(match) == MEM_read32(ip))) {
  596. size_t const h3 = ZSTD_hashPtr(ip+1, hBitsL, 8);
  597. U32 const matchIndex3 = hashLong[h3];
  598. const BYTE* const match3Base = matchIndex3 < prefixStartIndex ? dictBase : base;
  599. const BYTE* match3 = match3Base + matchIndex3;
  600. U32 offset;
  601. hashLong[h3] = curr + 1;
  602. if ( (matchIndex3 > dictStartIndex) && (MEM_read64(match3) == MEM_read64(ip+1)) ) {
  603. const BYTE* const matchEnd = matchIndex3 < prefixStartIndex ? dictEnd : iend;
  604. const BYTE* const lowMatchPtr = matchIndex3 < prefixStartIndex ? dictStart : prefixStart;
  605. mLength = ZSTD_count_2segments(ip+9, match3+8, iend, matchEnd, prefixStart) + 8;
  606. ip++;
  607. offset = curr+1 - matchIndex3;
  608. while (((ip>anchor) & (match3>lowMatchPtr)) && (ip[-1] == match3[-1])) { ip--; match3--; mLength++; } /* catch up */
  609. } else {
  610. const BYTE* const matchEnd = matchIndex < prefixStartIndex ? dictEnd : iend;
  611. const BYTE* const lowMatchPtr = matchIndex < prefixStartIndex ? dictStart : prefixStart;
  612. mLength = ZSTD_count_2segments(ip+4, match+4, iend, matchEnd, prefixStart) + 4;
  613. offset = curr - matchIndex;
  614. while (((ip>anchor) & (match>lowMatchPtr)) && (ip[-1] == match[-1])) { ip--; match--; mLength++; } /* catch up */
  615. }
  616. offset_2 = offset_1;
  617. offset_1 = offset;
  618. ZSTD_storeSeq(seqStore, (size_t)(ip-anchor), anchor, iend, OFFSET_TO_OFFBASE(offset), mLength);
  619. } else {
  620. ip += ((ip-anchor) >> kSearchStrength) + 1;
  621. continue;
  622. } }
  623. /* move to next sequence start */
  624. ip += mLength;
  625. anchor = ip;
  626. if (ip <= ilimit) {
  627. /* Complementary insertion */
  628. /* done after iLimit test, as candidates could be > iend-8 */
  629. { U32 const indexToInsert = curr+2;
  630. hashLong[ZSTD_hashPtr(base+indexToInsert, hBitsL, 8)] = indexToInsert;
  631. hashLong[ZSTD_hashPtr(ip-2, hBitsL, 8)] = (U32)(ip-2-base);
  632. hashSmall[ZSTD_hashPtr(base+indexToInsert, hBitsS, mls)] = indexToInsert;
  633. hashSmall[ZSTD_hashPtr(ip-1, hBitsS, mls)] = (U32)(ip-1-base);
  634. }
  635. /* check immediate repcode */
  636. while (ip <= ilimit) {
  637. U32 const current2 = (U32)(ip-base);
  638. U32 const repIndex2 = current2 - offset_2;
  639. const BYTE* repMatch2 = repIndex2 < prefixStartIndex ? dictBase + repIndex2 : base + repIndex2;
  640. if ( ((ZSTD_index_overlap_check(prefixStartIndex, repIndex2))
  641. & (offset_2 <= current2 - dictStartIndex))
  642. && (MEM_read32(repMatch2) == MEM_read32(ip)) ) {
  643. const BYTE* const repEnd2 = repIndex2 < prefixStartIndex ? dictEnd : iend;
  644. size_t const repLength2 = ZSTD_count_2segments(ip+4, repMatch2+4, iend, repEnd2, prefixStart) + 4;
  645. U32 const tmpOffset = offset_2; offset_2 = offset_1; offset_1 = tmpOffset; /* swap offset_2 <=> offset_1 */
  646. ZSTD_storeSeq(seqStore, 0, anchor, iend, REPCODE1_TO_OFFBASE, repLength2);
  647. hashSmall[ZSTD_hashPtr(ip, hBitsS, mls)] = current2;
  648. hashLong[ZSTD_hashPtr(ip, hBitsL, 8)] = current2;
  649. ip += repLength2;
  650. anchor = ip;
  651. continue;
  652. }
  653. break;
  654. } } }
  655. /* save reps for next block */
  656. rep[0] = offset_1;
  657. rep[1] = offset_2;
  658. /* Return the last literals size */
  659. return (size_t)(iend - anchor);
  660. }
  661. ZSTD_GEN_DFAST_FN(extDict, 4)
  662. ZSTD_GEN_DFAST_FN(extDict, 5)
  663. ZSTD_GEN_DFAST_FN(extDict, 6)
  664. ZSTD_GEN_DFAST_FN(extDict, 7)
  665. size_t ZSTD_compressBlock_doubleFast_extDict(
  666. ZSTD_MatchState_t* ms, SeqStore_t* seqStore, U32 rep[ZSTD_REP_NUM],
  667. void const* src, size_t srcSize)
  668. {
  669. U32 const mls = ms->cParams.minMatch;
  670. switch(mls)
  671. {
  672. default: /* includes case 3 */
  673. case 4 :
  674. return ZSTD_compressBlock_doubleFast_extDict_4(ms, seqStore, rep, src, srcSize);
  675. case 5 :
  676. return ZSTD_compressBlock_doubleFast_extDict_5(ms, seqStore, rep, src, srcSize);
  677. case 6 :
  678. return ZSTD_compressBlock_doubleFast_extDict_6(ms, seqStore, rep, src, srcSize);
  679. case 7 :
  680. return ZSTD_compressBlock_doubleFast_extDict_7(ms, seqStore, rep, src, srcSize);
  681. }
  682. }
  683. #endif /* ZSTD_EXCLUDE_DFAST_BLOCK_COMPRESSOR */