zstd_double_fast.c 35 KB

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