zstd_fast.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496
  1. /*
  2. * Copyright (c) Yann Collet, Facebook, Inc.
  3. * All rights reserved.
  4. *
  5. * This source code is licensed under both the BSD-style license (found in the
  6. * LICENSE file in the root directory of this source tree) and the GPLv2 (found
  7. * in the COPYING file in the root directory of this source tree).
  8. * You may select, at your option, one of the above-listed licenses.
  9. */
  10. #include "zstd_compress_internal.h" /* ZSTD_hashPtr, ZSTD_count, ZSTD_storeSeq */
  11. #include "zstd_fast.h"
  12. void ZSTD_fillHashTable(ZSTD_matchState_t* ms,
  13. const void* const end,
  14. ZSTD_dictTableLoadMethod_e dtlm)
  15. {
  16. const ZSTD_compressionParameters* const cParams = &ms->cParams;
  17. U32* const hashTable = ms->hashTable;
  18. U32 const hBits = cParams->hashLog;
  19. U32 const mls = cParams->minMatch;
  20. const BYTE* const base = ms->window.base;
  21. const BYTE* ip = base + ms->nextToUpdate;
  22. const BYTE* const iend = ((const BYTE*)end) - HASH_READ_SIZE;
  23. const U32 fastHashFillStep = 3;
  24. /* Always insert every fastHashFillStep position into the hash table.
  25. * Insert the other positions if their hash entry is empty.
  26. */
  27. for ( ; ip + fastHashFillStep < iend + 2; ip += fastHashFillStep) {
  28. U32 const curr = (U32)(ip - base);
  29. size_t const hash0 = ZSTD_hashPtr(ip, hBits, mls);
  30. hashTable[hash0] = curr;
  31. if (dtlm == ZSTD_dtlm_fast) continue;
  32. /* Only load extra positions for ZSTD_dtlm_full */
  33. { U32 p;
  34. for (p = 1; p < fastHashFillStep; ++p) {
  35. size_t const hash = ZSTD_hashPtr(ip + p, hBits, mls);
  36. if (hashTable[hash] == 0) { /* not yet filled */
  37. hashTable[hash] = curr + p;
  38. } } } }
  39. }
  40. FORCE_INLINE_TEMPLATE size_t
  41. ZSTD_compressBlock_fast_generic(
  42. ZSTD_matchState_t* ms, seqStore_t* seqStore, U32 rep[ZSTD_REP_NUM],
  43. void const* src, size_t srcSize,
  44. U32 const mls)
  45. {
  46. const ZSTD_compressionParameters* const cParams = &ms->cParams;
  47. U32* const hashTable = ms->hashTable;
  48. U32 const hlog = cParams->hashLog;
  49. /* support stepSize of 0 */
  50. size_t const stepSize = cParams->targetLength + !(cParams->targetLength) + 1;
  51. const BYTE* const base = ms->window.base;
  52. const BYTE* const istart = (const BYTE*)src;
  53. /* We check ip0 (ip + 0) and ip1 (ip + 1) each loop */
  54. const BYTE* ip0 = istart;
  55. const BYTE* ip1;
  56. const BYTE* anchor = istart;
  57. const U32 endIndex = (U32)((size_t)(istart - base) + srcSize);
  58. const U32 prefixStartIndex = ZSTD_getLowestPrefixIndex(ms, endIndex, cParams->windowLog);
  59. const BYTE* const prefixStart = base + prefixStartIndex;
  60. const BYTE* const iend = istart + srcSize;
  61. const BYTE* const ilimit = iend - HASH_READ_SIZE;
  62. U32 offset_1=rep[0], offset_2=rep[1];
  63. U32 offsetSaved = 0;
  64. /* init */
  65. DEBUGLOG(5, "ZSTD_compressBlock_fast_generic");
  66. ip0 += (ip0 == prefixStart);
  67. ip1 = ip0 + 1;
  68. { U32 const curr = (U32)(ip0 - base);
  69. U32 const windowLow = ZSTD_getLowestPrefixIndex(ms, curr, cParams->windowLog);
  70. U32 const maxRep = curr - windowLow;
  71. if (offset_2 > maxRep) offsetSaved = offset_2, offset_2 = 0;
  72. if (offset_1 > maxRep) offsetSaved = offset_1, offset_1 = 0;
  73. }
  74. /* Main Search Loop */
  75. #ifdef __INTEL_COMPILER
  76. /* From intel 'The vector pragma indicates that the loop should be
  77. * vectorized if it is legal to do so'. Can be used together with
  78. * #pragma ivdep (but have opted to exclude that because intel
  79. * warns against using it).*/
  80. #pragma vector always
  81. #endif
  82. while (ip1 < ilimit) { /* < instead of <=, because check at ip0+2 */
  83. size_t mLength;
  84. BYTE const* ip2 = ip0 + 2;
  85. size_t const h0 = ZSTD_hashPtr(ip0, hlog, mls);
  86. U32 const val0 = MEM_read32(ip0);
  87. size_t const h1 = ZSTD_hashPtr(ip1, hlog, mls);
  88. U32 const val1 = MEM_read32(ip1);
  89. U32 const current0 = (U32)(ip0-base);
  90. U32 const current1 = (U32)(ip1-base);
  91. U32 const matchIndex0 = hashTable[h0];
  92. U32 const matchIndex1 = hashTable[h1];
  93. BYTE const* repMatch = ip2 - offset_1;
  94. const BYTE* match0 = base + matchIndex0;
  95. const BYTE* match1 = base + matchIndex1;
  96. U32 offcode;
  97. #if defined(__aarch64__)
  98. PREFETCH_L1(ip0+256);
  99. #endif
  100. hashTable[h0] = current0; /* update hash table */
  101. hashTable[h1] = current1; /* update hash table */
  102. assert(ip0 + 1 == ip1);
  103. if ((offset_1 > 0) & (MEM_read32(repMatch) == MEM_read32(ip2))) {
  104. mLength = (ip2[-1] == repMatch[-1]) ? 1 : 0;
  105. ip0 = ip2 - mLength;
  106. match0 = repMatch - mLength;
  107. mLength += 4;
  108. offcode = 0;
  109. goto _match;
  110. }
  111. if ((matchIndex0 > prefixStartIndex) && MEM_read32(match0) == val0) {
  112. /* found a regular match */
  113. goto _offset;
  114. }
  115. if ((matchIndex1 > prefixStartIndex) && MEM_read32(match1) == val1) {
  116. /* found a regular match after one literal */
  117. ip0 = ip1;
  118. match0 = match1;
  119. goto _offset;
  120. }
  121. { size_t const step = ((size_t)(ip0-anchor) >> (kSearchStrength - 1)) + stepSize;
  122. assert(step >= 2);
  123. ip0 += step;
  124. ip1 += step;
  125. continue;
  126. }
  127. _offset: /* Requires: ip0, match0 */
  128. /* Compute the offset code */
  129. offset_2 = offset_1;
  130. offset_1 = (U32)(ip0-match0);
  131. offcode = offset_1 + ZSTD_REP_MOVE;
  132. mLength = 4;
  133. /* Count the backwards match length */
  134. while (((ip0>anchor) & (match0>prefixStart))
  135. && (ip0[-1] == match0[-1])) { ip0--; match0--; mLength++; } /* catch up */
  136. _match: /* Requires: ip0, match0, offcode */
  137. /* Count the forward length */
  138. mLength += ZSTD_count(ip0+mLength, match0+mLength, iend);
  139. ZSTD_storeSeq(seqStore, (size_t)(ip0-anchor), anchor, iend, offcode, mLength-MINMATCH);
  140. /* match found */
  141. ip0 += mLength;
  142. anchor = ip0;
  143. if (ip0 <= ilimit) {
  144. /* Fill Table */
  145. assert(base+current0+2 > istart); /* check base overflow */
  146. hashTable[ZSTD_hashPtr(base+current0+2, hlog, mls)] = current0+2; /* here because current+2 could be > iend-8 */
  147. hashTable[ZSTD_hashPtr(ip0-2, hlog, mls)] = (U32)(ip0-2-base);
  148. if (offset_2 > 0) { /* offset_2==0 means offset_2 is invalidated */
  149. while ( (ip0 <= ilimit) && (MEM_read32(ip0) == MEM_read32(ip0 - offset_2)) ) {
  150. /* store sequence */
  151. size_t const rLength = ZSTD_count(ip0+4, ip0+4-offset_2, iend) + 4;
  152. { U32 const tmpOff = offset_2; offset_2 = offset_1; offset_1 = tmpOff; } /* swap offset_2 <=> offset_1 */
  153. hashTable[ZSTD_hashPtr(ip0, hlog, mls)] = (U32)(ip0-base);
  154. ip0 += rLength;
  155. ZSTD_storeSeq(seqStore, 0 /*litLen*/, anchor, iend, 0 /*offCode*/, rLength-MINMATCH);
  156. anchor = ip0;
  157. continue; /* faster when present (confirmed on gcc-8) ... (?) */
  158. } } }
  159. ip1 = ip0 + 1;
  160. }
  161. /* save reps for next block */
  162. rep[0] = offset_1 ? offset_1 : offsetSaved;
  163. rep[1] = offset_2 ? offset_2 : offsetSaved;
  164. /* Return the last literals size */
  165. return (size_t)(iend - anchor);
  166. }
  167. size_t ZSTD_compressBlock_fast(
  168. ZSTD_matchState_t* ms, seqStore_t* seqStore, U32 rep[ZSTD_REP_NUM],
  169. void const* src, size_t srcSize)
  170. {
  171. U32 const mls = ms->cParams.minMatch;
  172. assert(ms->dictMatchState == NULL);
  173. switch(mls)
  174. {
  175. default: /* includes case 3 */
  176. case 4 :
  177. return ZSTD_compressBlock_fast_generic(ms, seqStore, rep, src, srcSize, 4);
  178. case 5 :
  179. return ZSTD_compressBlock_fast_generic(ms, seqStore, rep, src, srcSize, 5);
  180. case 6 :
  181. return ZSTD_compressBlock_fast_generic(ms, seqStore, rep, src, srcSize, 6);
  182. case 7 :
  183. return ZSTD_compressBlock_fast_generic(ms, seqStore, rep, src, srcSize, 7);
  184. }
  185. }
  186. FORCE_INLINE_TEMPLATE
  187. size_t ZSTD_compressBlock_fast_dictMatchState_generic(
  188. ZSTD_matchState_t* ms, seqStore_t* seqStore, U32 rep[ZSTD_REP_NUM],
  189. void const* src, size_t srcSize, U32 const mls)
  190. {
  191. const ZSTD_compressionParameters* const cParams = &ms->cParams;
  192. U32* const hashTable = ms->hashTable;
  193. U32 const hlog = cParams->hashLog;
  194. /* support stepSize of 0 */
  195. U32 const stepSize = cParams->targetLength + !(cParams->targetLength);
  196. const BYTE* const base = ms->window.base;
  197. const BYTE* const istart = (const BYTE*)src;
  198. const BYTE* ip = istart;
  199. const BYTE* anchor = istart;
  200. const U32 prefixStartIndex = ms->window.dictLimit;
  201. const BYTE* const prefixStart = base + prefixStartIndex;
  202. const BYTE* const iend = istart + srcSize;
  203. const BYTE* const ilimit = iend - HASH_READ_SIZE;
  204. U32 offset_1=rep[0], offset_2=rep[1];
  205. U32 offsetSaved = 0;
  206. const ZSTD_matchState_t* const dms = ms->dictMatchState;
  207. const ZSTD_compressionParameters* const dictCParams = &dms->cParams ;
  208. const U32* const dictHashTable = dms->hashTable;
  209. const U32 dictStartIndex = dms->window.dictLimit;
  210. const BYTE* const dictBase = dms->window.base;
  211. const BYTE* const dictStart = dictBase + dictStartIndex;
  212. const BYTE* const dictEnd = dms->window.nextSrc;
  213. const U32 dictIndexDelta = prefixStartIndex - (U32)(dictEnd - dictBase);
  214. const U32 dictAndPrefixLength = (U32)(ip - prefixStart + dictEnd - dictStart);
  215. const U32 dictHLog = dictCParams->hashLog;
  216. /* if a dictionary is still attached, it necessarily means that
  217. * it is within window size. So we just check it. */
  218. const U32 maxDistance = 1U << cParams->windowLog;
  219. const U32 endIndex = (U32)((size_t)(ip - base) + srcSize);
  220. assert(endIndex - prefixStartIndex <= maxDistance);
  221. (void)maxDistance; (void)endIndex; /* these variables are not used when assert() is disabled */
  222. /* ensure there will be no underflow
  223. * when translating a dict index into a local index */
  224. assert(prefixStartIndex >= (U32)(dictEnd - dictBase));
  225. /* init */
  226. DEBUGLOG(5, "ZSTD_compressBlock_fast_dictMatchState_generic");
  227. ip += (dictAndPrefixLength == 0);
  228. /* dictMatchState repCode checks don't currently handle repCode == 0
  229. * disabling. */
  230. assert(offset_1 <= dictAndPrefixLength);
  231. assert(offset_2 <= dictAndPrefixLength);
  232. /* Main Search Loop */
  233. while (ip < ilimit) { /* < instead of <=, because repcode check at (ip+1) */
  234. size_t mLength;
  235. size_t const h = ZSTD_hashPtr(ip, hlog, mls);
  236. U32 const curr = (U32)(ip-base);
  237. U32 const matchIndex = hashTable[h];
  238. const BYTE* match = base + matchIndex;
  239. const U32 repIndex = curr + 1 - offset_1;
  240. const BYTE* repMatch = (repIndex < prefixStartIndex) ?
  241. dictBase + (repIndex - dictIndexDelta) :
  242. base + repIndex;
  243. hashTable[h] = curr; /* update hash table */
  244. if ( ((U32)((prefixStartIndex-1) - repIndex) >= 3) /* intentional underflow : ensure repIndex isn't overlapping dict + prefix */
  245. && (MEM_read32(repMatch) == MEM_read32(ip+1)) ) {
  246. const BYTE* const repMatchEnd = repIndex < prefixStartIndex ? dictEnd : iend;
  247. mLength = ZSTD_count_2segments(ip+1+4, repMatch+4, iend, repMatchEnd, prefixStart) + 4;
  248. ip++;
  249. ZSTD_storeSeq(seqStore, (size_t)(ip-anchor), anchor, iend, 0, mLength-MINMATCH);
  250. } else if ( (matchIndex <= prefixStartIndex) ) {
  251. size_t const dictHash = ZSTD_hashPtr(ip, dictHLog, mls);
  252. U32 const dictMatchIndex = dictHashTable[dictHash];
  253. const BYTE* dictMatch = dictBase + dictMatchIndex;
  254. if (dictMatchIndex <= dictStartIndex ||
  255. MEM_read32(dictMatch) != MEM_read32(ip)) {
  256. assert(stepSize >= 1);
  257. ip += ((ip-anchor) >> kSearchStrength) + stepSize;
  258. continue;
  259. } else {
  260. /* found a dict match */
  261. U32 const offset = (U32)(curr-dictMatchIndex-dictIndexDelta);
  262. mLength = ZSTD_count_2segments(ip+4, dictMatch+4, iend, dictEnd, prefixStart) + 4;
  263. while (((ip>anchor) & (dictMatch>dictStart))
  264. && (ip[-1] == dictMatch[-1])) {
  265. ip--; dictMatch--; mLength++;
  266. } /* catch up */
  267. offset_2 = offset_1;
  268. offset_1 = offset;
  269. ZSTD_storeSeq(seqStore, (size_t)(ip-anchor), anchor, iend, offset + ZSTD_REP_MOVE, mLength-MINMATCH);
  270. }
  271. } else if (MEM_read32(match) != MEM_read32(ip)) {
  272. /* it's not a match, and we're not going to check the dictionary */
  273. assert(stepSize >= 1);
  274. ip += ((ip-anchor) >> kSearchStrength) + stepSize;
  275. continue;
  276. } else {
  277. /* found a regular match */
  278. U32 const offset = (U32)(ip-match);
  279. mLength = ZSTD_count(ip+4, match+4, iend) + 4;
  280. while (((ip>anchor) & (match>prefixStart))
  281. && (ip[-1] == match[-1])) { ip--; match--; mLength++; } /* catch up */
  282. offset_2 = offset_1;
  283. offset_1 = offset;
  284. ZSTD_storeSeq(seqStore, (size_t)(ip-anchor), anchor, iend, offset + ZSTD_REP_MOVE, mLength-MINMATCH);
  285. }
  286. /* match found */
  287. ip += mLength;
  288. anchor = ip;
  289. if (ip <= ilimit) {
  290. /* Fill Table */
  291. assert(base+curr+2 > istart); /* check base overflow */
  292. hashTable[ZSTD_hashPtr(base+curr+2, hlog, mls)] = curr+2; /* here because curr+2 could be > iend-8 */
  293. hashTable[ZSTD_hashPtr(ip-2, hlog, mls)] = (U32)(ip-2-base);
  294. /* check immediate repcode */
  295. while (ip <= ilimit) {
  296. U32 const current2 = (U32)(ip-base);
  297. U32 const repIndex2 = current2 - offset_2;
  298. const BYTE* repMatch2 = repIndex2 < prefixStartIndex ?
  299. dictBase - dictIndexDelta + repIndex2 :
  300. base + repIndex2;
  301. if ( ((U32)((prefixStartIndex-1) - (U32)repIndex2) >= 3 /* intentional overflow */)
  302. && (MEM_read32(repMatch2) == MEM_read32(ip)) ) {
  303. const BYTE* const repEnd2 = repIndex2 < prefixStartIndex ? dictEnd : iend;
  304. size_t const repLength2 = ZSTD_count_2segments(ip+4, repMatch2+4, iend, repEnd2, prefixStart) + 4;
  305. U32 tmpOffset = offset_2; offset_2 = offset_1; offset_1 = tmpOffset; /* swap offset_2 <=> offset_1 */
  306. ZSTD_storeSeq(seqStore, 0, anchor, iend, 0, repLength2-MINMATCH);
  307. hashTable[ZSTD_hashPtr(ip, hlog, mls)] = current2;
  308. ip += repLength2;
  309. anchor = ip;
  310. continue;
  311. }
  312. break;
  313. }
  314. }
  315. }
  316. /* save reps for next block */
  317. rep[0] = offset_1 ? offset_1 : offsetSaved;
  318. rep[1] = offset_2 ? offset_2 : offsetSaved;
  319. /* Return the last literals size */
  320. return (size_t)(iend - anchor);
  321. }
  322. size_t ZSTD_compressBlock_fast_dictMatchState(
  323. ZSTD_matchState_t* ms, seqStore_t* seqStore, U32 rep[ZSTD_REP_NUM],
  324. void const* src, size_t srcSize)
  325. {
  326. U32 const mls = ms->cParams.minMatch;
  327. assert(ms->dictMatchState != NULL);
  328. switch(mls)
  329. {
  330. default: /* includes case 3 */
  331. case 4 :
  332. return ZSTD_compressBlock_fast_dictMatchState_generic(ms, seqStore, rep, src, srcSize, 4);
  333. case 5 :
  334. return ZSTD_compressBlock_fast_dictMatchState_generic(ms, seqStore, rep, src, srcSize, 5);
  335. case 6 :
  336. return ZSTD_compressBlock_fast_dictMatchState_generic(ms, seqStore, rep, src, srcSize, 6);
  337. case 7 :
  338. return ZSTD_compressBlock_fast_dictMatchState_generic(ms, seqStore, rep, src, srcSize, 7);
  339. }
  340. }
  341. static size_t ZSTD_compressBlock_fast_extDict_generic(
  342. ZSTD_matchState_t* ms, seqStore_t* seqStore, U32 rep[ZSTD_REP_NUM],
  343. void const* src, size_t srcSize, U32 const mls)
  344. {
  345. const ZSTD_compressionParameters* const cParams = &ms->cParams;
  346. U32* const hashTable = ms->hashTable;
  347. U32 const hlog = cParams->hashLog;
  348. /* support stepSize of 0 */
  349. U32 const stepSize = cParams->targetLength + !(cParams->targetLength);
  350. const BYTE* const base = ms->window.base;
  351. const BYTE* const dictBase = ms->window.dictBase;
  352. const BYTE* const istart = (const BYTE*)src;
  353. const BYTE* ip = istart;
  354. const BYTE* anchor = istart;
  355. const U32 endIndex = (U32)((size_t)(istart - base) + srcSize);
  356. const U32 lowLimit = ZSTD_getLowestMatchIndex(ms, endIndex, cParams->windowLog);
  357. const U32 dictStartIndex = lowLimit;
  358. const BYTE* const dictStart = dictBase + dictStartIndex;
  359. const U32 dictLimit = ms->window.dictLimit;
  360. const U32 prefixStartIndex = dictLimit < lowLimit ? lowLimit : dictLimit;
  361. const BYTE* const prefixStart = base + prefixStartIndex;
  362. const BYTE* const dictEnd = dictBase + prefixStartIndex;
  363. const BYTE* const iend = istart + srcSize;
  364. const BYTE* const ilimit = iend - 8;
  365. U32 offset_1=rep[0], offset_2=rep[1];
  366. DEBUGLOG(5, "ZSTD_compressBlock_fast_extDict_generic (offset_1=%u)", offset_1);
  367. /* switch to "regular" variant if extDict is invalidated due to maxDistance */
  368. if (prefixStartIndex == dictStartIndex)
  369. return ZSTD_compressBlock_fast_generic(ms, seqStore, rep, src, srcSize, mls);
  370. /* Search Loop */
  371. while (ip < ilimit) { /* < instead of <=, because (ip+1) */
  372. const size_t h = ZSTD_hashPtr(ip, hlog, mls);
  373. const U32 matchIndex = hashTable[h];
  374. const BYTE* const matchBase = matchIndex < prefixStartIndex ? dictBase : base;
  375. const BYTE* match = matchBase + matchIndex;
  376. const U32 curr = (U32)(ip-base);
  377. const U32 repIndex = curr + 1 - offset_1;
  378. const BYTE* const repBase = repIndex < prefixStartIndex ? dictBase : base;
  379. const BYTE* const repMatch = repBase + repIndex;
  380. hashTable[h] = curr; /* update hash table */
  381. DEBUGLOG(7, "offset_1 = %u , curr = %u", offset_1, curr);
  382. assert(offset_1 <= curr +1); /* check repIndex */
  383. if ( (((U32)((prefixStartIndex-1) - repIndex) >= 3) /* intentional underflow */ & (repIndex > dictStartIndex))
  384. && (MEM_read32(repMatch) == MEM_read32(ip+1)) ) {
  385. const BYTE* const repMatchEnd = repIndex < prefixStartIndex ? dictEnd : iend;
  386. size_t const rLength = ZSTD_count_2segments(ip+1 +4, repMatch +4, iend, repMatchEnd, prefixStart) + 4;
  387. ip++;
  388. ZSTD_storeSeq(seqStore, (size_t)(ip-anchor), anchor, iend, 0, rLength-MINMATCH);
  389. ip += rLength;
  390. anchor = ip;
  391. } else {
  392. if ( (matchIndex < dictStartIndex) ||
  393. (MEM_read32(match) != MEM_read32(ip)) ) {
  394. assert(stepSize >= 1);
  395. ip += ((ip-anchor) >> kSearchStrength) + stepSize;
  396. continue;
  397. }
  398. { const BYTE* const matchEnd = matchIndex < prefixStartIndex ? dictEnd : iend;
  399. const BYTE* const lowMatchPtr = matchIndex < prefixStartIndex ? dictStart : prefixStart;
  400. U32 const offset = curr - matchIndex;
  401. size_t mLength = ZSTD_count_2segments(ip+4, match+4, iend, matchEnd, prefixStart) + 4;
  402. while (((ip>anchor) & (match>lowMatchPtr)) && (ip[-1] == match[-1])) { ip--; match--; mLength++; } /* catch up */
  403. offset_2 = offset_1; offset_1 = offset; /* update offset history */
  404. ZSTD_storeSeq(seqStore, (size_t)(ip-anchor), anchor, iend, offset + ZSTD_REP_MOVE, mLength-MINMATCH);
  405. ip += mLength;
  406. anchor = ip;
  407. } }
  408. if (ip <= ilimit) {
  409. /* Fill Table */
  410. hashTable[ZSTD_hashPtr(base+curr+2, hlog, mls)] = curr+2;
  411. hashTable[ZSTD_hashPtr(ip-2, hlog, mls)] = (U32)(ip-2-base);
  412. /* check immediate repcode */
  413. while (ip <= ilimit) {
  414. U32 const current2 = (U32)(ip-base);
  415. U32 const repIndex2 = current2 - offset_2;
  416. const BYTE* const repMatch2 = repIndex2 < prefixStartIndex ? dictBase + repIndex2 : base + repIndex2;
  417. if ( (((U32)((prefixStartIndex-1) - repIndex2) >= 3) & (repIndex2 > dictStartIndex)) /* intentional overflow */
  418. && (MEM_read32(repMatch2) == MEM_read32(ip)) ) {
  419. const BYTE* const repEnd2 = repIndex2 < prefixStartIndex ? dictEnd : iend;
  420. size_t const repLength2 = ZSTD_count_2segments(ip+4, repMatch2+4, iend, repEnd2, prefixStart) + 4;
  421. { U32 const tmpOffset = offset_2; offset_2 = offset_1; offset_1 = tmpOffset; } /* swap offset_2 <=> offset_1 */
  422. ZSTD_storeSeq(seqStore, 0 /*litlen*/, anchor, iend, 0 /*offcode*/, repLength2-MINMATCH);
  423. hashTable[ZSTD_hashPtr(ip, hlog, mls)] = current2;
  424. ip += repLength2;
  425. anchor = ip;
  426. continue;
  427. }
  428. break;
  429. } } }
  430. /* save reps for next block */
  431. rep[0] = offset_1;
  432. rep[1] = offset_2;
  433. /* Return the last literals size */
  434. return (size_t)(iend - anchor);
  435. }
  436. size_t ZSTD_compressBlock_fast_extDict(
  437. ZSTD_matchState_t* ms, seqStore_t* seqStore, U32 rep[ZSTD_REP_NUM],
  438. void const* src, size_t srcSize)
  439. {
  440. U32 const mls = ms->cParams.minMatch;
  441. switch(mls)
  442. {
  443. default: /* includes case 3 */
  444. case 4 :
  445. return ZSTD_compressBlock_fast_extDict_generic(ms, seqStore, rep, src, srcSize, 4);
  446. case 5 :
  447. return ZSTD_compressBlock_fast_extDict_generic(ms, seqStore, rep, src, srcSize, 5);
  448. case 6 :
  449. return ZSTD_compressBlock_fast_extDict_generic(ms, seqStore, rep, src, srcSize, 6);
  450. case 7 :
  451. return ZSTD_compressBlock_fast_extDict_generic(ms, seqStore, rep, src, srcSize, 7);
  452. }
  453. }