zstd_fast.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  1. /*
  2. * Copyright (c) 2016-present, 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 current = (U32)(ip - base);
  29. size_t const hash0 = ZSTD_hashPtr(ip, hBits, mls);
  30. hashTable[hash0] = current;
  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] = current + 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 maxDistance = 1U << cParams->windowLog;
  59. const U32 validStartIndex = ms->window.dictLimit;
  60. const U32 prefixStartIndex = (endIndex - validStartIndex > maxDistance) ? endIndex - maxDistance : validStartIndex;
  61. const BYTE* const prefixStart = base + prefixStartIndex;
  62. const BYTE* const iend = istart + srcSize;
  63. const BYTE* const ilimit = iend - HASH_READ_SIZE;
  64. U32 offset_1=rep[0], offset_2=rep[1];
  65. U32 offsetSaved = 0;
  66. /* init */
  67. DEBUGLOG(5, "ZSTD_compressBlock_fast_generic");
  68. ip0 += (ip0 == prefixStart);
  69. ip1 = ip0 + 1;
  70. { U32 const maxRep = (U32)(ip0 - prefixStart);
  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. while (ip1 < ilimit) { /* < instead of <=, because check at ip0+2 */
  76. size_t mLength;
  77. BYTE const* ip2 = ip0 + 2;
  78. size_t const h0 = ZSTD_hashPtr(ip0, hlog, mls);
  79. U32 const val0 = MEM_read32(ip0);
  80. size_t const h1 = ZSTD_hashPtr(ip1, hlog, mls);
  81. U32 const val1 = MEM_read32(ip1);
  82. U32 const current0 = (U32)(ip0-base);
  83. U32 const current1 = (U32)(ip1-base);
  84. U32 const matchIndex0 = hashTable[h0];
  85. U32 const matchIndex1 = hashTable[h1];
  86. BYTE const* repMatch = ip2-offset_1;
  87. const BYTE* match0 = base + matchIndex0;
  88. const BYTE* match1 = base + matchIndex1;
  89. U32 offcode;
  90. hashTable[h0] = current0; /* update hash table */
  91. hashTable[h1] = current1; /* update hash table */
  92. assert(ip0 + 1 == ip1);
  93. if ((offset_1 > 0) & (MEM_read32(repMatch) == MEM_read32(ip2))) {
  94. mLength = ip2[-1] == repMatch[-1] ? 1 : 0;
  95. ip0 = ip2 - mLength;
  96. match0 = repMatch - mLength;
  97. offcode = 0;
  98. goto _match;
  99. }
  100. if ((matchIndex0 > prefixStartIndex) && MEM_read32(match0) == val0) {
  101. /* found a regular match */
  102. goto _offset;
  103. }
  104. if ((matchIndex1 > prefixStartIndex) && MEM_read32(match1) == val1) {
  105. /* found a regular match after one literal */
  106. ip0 = ip1;
  107. match0 = match1;
  108. goto _offset;
  109. }
  110. { size_t const step = ((size_t)(ip0-anchor) >> (kSearchStrength - 1)) + stepSize;
  111. assert(step >= 2);
  112. ip0 += step;
  113. ip1 += step;
  114. continue;
  115. }
  116. _offset: /* Requires: ip0, match0 */
  117. /* Compute the offset code */
  118. offset_2 = offset_1;
  119. offset_1 = (U32)(ip0-match0);
  120. offcode = offset_1 + ZSTD_REP_MOVE;
  121. mLength = 0;
  122. /* Count the backwards match length */
  123. while (((ip0>anchor) & (match0>prefixStart))
  124. && (ip0[-1] == match0[-1])) { ip0--; match0--; mLength++; } /* catch up */
  125. _match: /* Requires: ip0, match0, offcode */
  126. /* Count the forward length */
  127. mLength += ZSTD_count(ip0+mLength+4, match0+mLength+4, iend) + 4;
  128. ZSTD_storeSeq(seqStore, (size_t)(ip0-anchor), anchor, iend, offcode, mLength-MINMATCH);
  129. /* match found */
  130. ip0 += mLength;
  131. anchor = ip0;
  132. ip1 = ip0 + 1;
  133. if (ip0 <= ilimit) {
  134. /* Fill Table */
  135. assert(base+current0+2 > istart); /* check base overflow */
  136. hashTable[ZSTD_hashPtr(base+current0+2, hlog, mls)] = current0+2; /* here because current+2 could be > iend-8 */
  137. hashTable[ZSTD_hashPtr(ip0-2, hlog, mls)] = (U32)(ip0-2-base);
  138. while ( ((ip0 <= ilimit) & (offset_2>0)) /* offset_2==0 means offset_2 is invalidated */
  139. && (MEM_read32(ip0) == MEM_read32(ip0 - offset_2)) ) {
  140. /* store sequence */
  141. size_t const rLength = ZSTD_count(ip0+4, ip0+4-offset_2, iend) + 4;
  142. { U32 const tmpOff = offset_2; offset_2 = offset_1; offset_1 = tmpOff; } /* swap offset_2 <=> offset_1 */
  143. hashTable[ZSTD_hashPtr(ip0, hlog, mls)] = (U32)(ip0-base);
  144. ip0 += rLength;
  145. ip1 = ip0 + 1;
  146. ZSTD_storeSeq(seqStore, 0 /*litLen*/, anchor, iend, 0 /*offCode*/, rLength-MINMATCH);
  147. anchor = ip0;
  148. continue; /* faster when present (confirmed on gcc-8) ... (?) */
  149. }
  150. }
  151. }
  152. /* save reps for next block */
  153. rep[0] = offset_1 ? offset_1 : offsetSaved;
  154. rep[1] = offset_2 ? offset_2 : offsetSaved;
  155. /* Return the last literals size */
  156. return (size_t)(iend - anchor);
  157. }
  158. size_t ZSTD_compressBlock_fast(
  159. ZSTD_matchState_t* ms, seqStore_t* seqStore, U32 rep[ZSTD_REP_NUM],
  160. void const* src, size_t srcSize)
  161. {
  162. U32 const mls = ms->cParams.minMatch;
  163. assert(ms->dictMatchState == NULL);
  164. switch(mls)
  165. {
  166. default: /* includes case 3 */
  167. case 4 :
  168. return ZSTD_compressBlock_fast_generic(ms, seqStore, rep, src, srcSize, 4);
  169. case 5 :
  170. return ZSTD_compressBlock_fast_generic(ms, seqStore, rep, src, srcSize, 5);
  171. case 6 :
  172. return ZSTD_compressBlock_fast_generic(ms, seqStore, rep, src, srcSize, 6);
  173. case 7 :
  174. return ZSTD_compressBlock_fast_generic(ms, seqStore, rep, src, srcSize, 7);
  175. }
  176. }
  177. FORCE_INLINE_TEMPLATE
  178. size_t ZSTD_compressBlock_fast_dictMatchState_generic(
  179. ZSTD_matchState_t* ms, seqStore_t* seqStore, U32 rep[ZSTD_REP_NUM],
  180. void const* src, size_t srcSize, U32 const mls)
  181. {
  182. const ZSTD_compressionParameters* const cParams = &ms->cParams;
  183. U32* const hashTable = ms->hashTable;
  184. U32 const hlog = cParams->hashLog;
  185. /* support stepSize of 0 */
  186. U32 const stepSize = cParams->targetLength + !(cParams->targetLength);
  187. const BYTE* const base = ms->window.base;
  188. const BYTE* const istart = (const BYTE*)src;
  189. const BYTE* ip = istart;
  190. const BYTE* anchor = istart;
  191. const U32 prefixStartIndex = ms->window.dictLimit;
  192. const BYTE* const prefixStart = base + prefixStartIndex;
  193. const BYTE* const iend = istart + srcSize;
  194. const BYTE* const ilimit = iend - HASH_READ_SIZE;
  195. U32 offset_1=rep[0], offset_2=rep[1];
  196. U32 offsetSaved = 0;
  197. const ZSTD_matchState_t* const dms = ms->dictMatchState;
  198. const ZSTD_compressionParameters* const dictCParams = &dms->cParams ;
  199. const U32* const dictHashTable = dms->hashTable;
  200. const U32 dictStartIndex = dms->window.dictLimit;
  201. const BYTE* const dictBase = dms->window.base;
  202. const BYTE* const dictStart = dictBase + dictStartIndex;
  203. const BYTE* const dictEnd = dms->window.nextSrc;
  204. const U32 dictIndexDelta = prefixStartIndex - (U32)(dictEnd - dictBase);
  205. const U32 dictAndPrefixLength = (U32)(ip - prefixStart + dictEnd - dictStart);
  206. const U32 dictHLog = dictCParams->hashLog;
  207. /* if a dictionary is still attached, it necessarily means that
  208. * it is within window size. So we just check it. */
  209. const U32 maxDistance = 1U << cParams->windowLog;
  210. const U32 endIndex = (U32)((size_t)(ip - base) + srcSize);
  211. assert(endIndex - prefixStartIndex <= maxDistance);
  212. (void)maxDistance; (void)endIndex; /* these variables are not used when assert() is disabled */
  213. /* ensure there will be no no underflow
  214. * when translating a dict index into a local index */
  215. assert(prefixStartIndex >= (U32)(dictEnd - dictBase));
  216. /* init */
  217. DEBUGLOG(5, "ZSTD_compressBlock_fast_dictMatchState_generic");
  218. ip += (dictAndPrefixLength == 0);
  219. /* dictMatchState repCode checks don't currently handle repCode == 0
  220. * disabling. */
  221. assert(offset_1 <= dictAndPrefixLength);
  222. assert(offset_2 <= dictAndPrefixLength);
  223. /* Main Search Loop */
  224. while (ip < ilimit) { /* < instead of <=, because repcode check at (ip+1) */
  225. size_t mLength;
  226. size_t const h = ZSTD_hashPtr(ip, hlog, mls);
  227. U32 const current = (U32)(ip-base);
  228. U32 const matchIndex = hashTable[h];
  229. const BYTE* match = base + matchIndex;
  230. const U32 repIndex = current + 1 - offset_1;
  231. const BYTE* repMatch = (repIndex < prefixStartIndex) ?
  232. dictBase + (repIndex - dictIndexDelta) :
  233. base + repIndex;
  234. hashTable[h] = current; /* update hash table */
  235. if ( ((U32)((prefixStartIndex-1) - repIndex) >= 3) /* intentional underflow : ensure repIndex isn't overlapping dict + prefix */
  236. && (MEM_read32(repMatch) == MEM_read32(ip+1)) ) {
  237. const BYTE* const repMatchEnd = repIndex < prefixStartIndex ? dictEnd : iend;
  238. mLength = ZSTD_count_2segments(ip+1+4, repMatch+4, iend, repMatchEnd, prefixStart) + 4;
  239. ip++;
  240. ZSTD_storeSeq(seqStore, (size_t)(ip-anchor), anchor, iend, 0, mLength-MINMATCH);
  241. } else if ( (matchIndex <= prefixStartIndex) ) {
  242. size_t const dictHash = ZSTD_hashPtr(ip, dictHLog, mls);
  243. U32 const dictMatchIndex = dictHashTable[dictHash];
  244. const BYTE* dictMatch = dictBase + dictMatchIndex;
  245. if (dictMatchIndex <= dictStartIndex ||
  246. MEM_read32(dictMatch) != MEM_read32(ip)) {
  247. assert(stepSize >= 1);
  248. ip += ((ip-anchor) >> kSearchStrength) + stepSize;
  249. continue;
  250. } else {
  251. /* found a dict match */
  252. U32 const offset = (U32)(current-dictMatchIndex-dictIndexDelta);
  253. mLength = ZSTD_count_2segments(ip+4, dictMatch+4, iend, dictEnd, prefixStart) + 4;
  254. while (((ip>anchor) & (dictMatch>dictStart))
  255. && (ip[-1] == dictMatch[-1])) {
  256. ip--; dictMatch--; mLength++;
  257. } /* catch up */
  258. offset_2 = offset_1;
  259. offset_1 = offset;
  260. ZSTD_storeSeq(seqStore, (size_t)(ip-anchor), anchor, iend, offset + ZSTD_REP_MOVE, mLength-MINMATCH);
  261. }
  262. } else if (MEM_read32(match) != MEM_read32(ip)) {
  263. /* it's not a match, and we're not going to check the dictionary */
  264. assert(stepSize >= 1);
  265. ip += ((ip-anchor) >> kSearchStrength) + stepSize;
  266. continue;
  267. } else {
  268. /* found a regular match */
  269. U32 const offset = (U32)(ip-match);
  270. mLength = ZSTD_count(ip+4, match+4, iend) + 4;
  271. while (((ip>anchor) & (match>prefixStart))
  272. && (ip[-1] == match[-1])) { ip--; match--; mLength++; } /* catch up */
  273. offset_2 = offset_1;
  274. offset_1 = offset;
  275. ZSTD_storeSeq(seqStore, (size_t)(ip-anchor), anchor, iend, offset + ZSTD_REP_MOVE, mLength-MINMATCH);
  276. }
  277. /* match found */
  278. ip += mLength;
  279. anchor = ip;
  280. if (ip <= ilimit) {
  281. /* Fill Table */
  282. assert(base+current+2 > istart); /* check base overflow */
  283. hashTable[ZSTD_hashPtr(base+current+2, hlog, mls)] = current+2; /* here because current+2 could be > iend-8 */
  284. hashTable[ZSTD_hashPtr(ip-2, hlog, mls)] = (U32)(ip-2-base);
  285. /* check immediate repcode */
  286. while (ip <= ilimit) {
  287. U32 const current2 = (U32)(ip-base);
  288. U32 const repIndex2 = current2 - offset_2;
  289. const BYTE* repMatch2 = repIndex2 < prefixStartIndex ?
  290. dictBase - dictIndexDelta + repIndex2 :
  291. base + repIndex2;
  292. if ( ((U32)((prefixStartIndex-1) - (U32)repIndex2) >= 3 /* intentional overflow */)
  293. && (MEM_read32(repMatch2) == MEM_read32(ip)) ) {
  294. const BYTE* const repEnd2 = repIndex2 < prefixStartIndex ? dictEnd : iend;
  295. size_t const repLength2 = ZSTD_count_2segments(ip+4, repMatch2+4, iend, repEnd2, prefixStart) + 4;
  296. U32 tmpOffset = offset_2; offset_2 = offset_1; offset_1 = tmpOffset; /* swap offset_2 <=> offset_1 */
  297. ZSTD_storeSeq(seqStore, 0, anchor, iend, 0, repLength2-MINMATCH);
  298. hashTable[ZSTD_hashPtr(ip, hlog, mls)] = current2;
  299. ip += repLength2;
  300. anchor = ip;
  301. continue;
  302. }
  303. break;
  304. }
  305. }
  306. }
  307. /* save reps for next block */
  308. rep[0] = offset_1 ? offset_1 : offsetSaved;
  309. rep[1] = offset_2 ? offset_2 : offsetSaved;
  310. /* Return the last literals size */
  311. return (size_t)(iend - anchor);
  312. }
  313. size_t ZSTD_compressBlock_fast_dictMatchState(
  314. ZSTD_matchState_t* ms, seqStore_t* seqStore, U32 rep[ZSTD_REP_NUM],
  315. void const* src, size_t srcSize)
  316. {
  317. U32 const mls = ms->cParams.minMatch;
  318. assert(ms->dictMatchState != NULL);
  319. switch(mls)
  320. {
  321. default: /* includes case 3 */
  322. case 4 :
  323. return ZSTD_compressBlock_fast_dictMatchState_generic(ms, seqStore, rep, src, srcSize, 4);
  324. case 5 :
  325. return ZSTD_compressBlock_fast_dictMatchState_generic(ms, seqStore, rep, src, srcSize, 5);
  326. case 6 :
  327. return ZSTD_compressBlock_fast_dictMatchState_generic(ms, seqStore, rep, src, srcSize, 6);
  328. case 7 :
  329. return ZSTD_compressBlock_fast_dictMatchState_generic(ms, seqStore, rep, src, srcSize, 7);
  330. }
  331. }
  332. static size_t ZSTD_compressBlock_fast_extDict_generic(
  333. ZSTD_matchState_t* ms, seqStore_t* seqStore, U32 rep[ZSTD_REP_NUM],
  334. void const* src, size_t srcSize, U32 const mls)
  335. {
  336. const ZSTD_compressionParameters* const cParams = &ms->cParams;
  337. U32* const hashTable = ms->hashTable;
  338. U32 const hlog = cParams->hashLog;
  339. /* support stepSize of 0 */
  340. U32 const stepSize = cParams->targetLength + !(cParams->targetLength);
  341. const BYTE* const base = ms->window.base;
  342. const BYTE* const dictBase = ms->window.dictBase;
  343. const BYTE* const istart = (const BYTE*)src;
  344. const BYTE* ip = istart;
  345. const BYTE* anchor = istart;
  346. const U32 endIndex = (U32)((size_t)(istart - base) + srcSize);
  347. const U32 lowLimit = ZSTD_getLowestMatchIndex(ms, endIndex, cParams->windowLog);
  348. const U32 dictStartIndex = lowLimit;
  349. const BYTE* const dictStart = dictBase + dictStartIndex;
  350. const U32 dictLimit = ms->window.dictLimit;
  351. const U32 prefixStartIndex = dictLimit < lowLimit ? lowLimit : dictLimit;
  352. const BYTE* const prefixStart = base + prefixStartIndex;
  353. const BYTE* const dictEnd = dictBase + prefixStartIndex;
  354. const BYTE* const iend = istart + srcSize;
  355. const BYTE* const ilimit = iend - 8;
  356. U32 offset_1=rep[0], offset_2=rep[1];
  357. DEBUGLOG(5, "ZSTD_compressBlock_fast_extDict_generic");
  358. /* switch to "regular" variant if extDict is invalidated due to maxDistance */
  359. if (prefixStartIndex == dictStartIndex)
  360. return ZSTD_compressBlock_fast_generic(ms, seqStore, rep, src, srcSize, mls);
  361. /* Search Loop */
  362. while (ip < ilimit) { /* < instead of <=, because (ip+1) */
  363. const size_t h = ZSTD_hashPtr(ip, hlog, mls);
  364. const U32 matchIndex = hashTable[h];
  365. const BYTE* const matchBase = matchIndex < prefixStartIndex ? dictBase : base;
  366. const BYTE* match = matchBase + matchIndex;
  367. const U32 current = (U32)(ip-base);
  368. const U32 repIndex = current + 1 - offset_1;
  369. const BYTE* const repBase = repIndex < prefixStartIndex ? dictBase : base;
  370. const BYTE* const repMatch = repBase + repIndex;
  371. hashTable[h] = current; /* update hash table */
  372. assert(offset_1 <= current +1); /* check repIndex */
  373. if ( (((U32)((prefixStartIndex-1) - repIndex) >= 3) /* intentional underflow */ & (repIndex > dictStartIndex))
  374. && (MEM_read32(repMatch) == MEM_read32(ip+1)) ) {
  375. const BYTE* const repMatchEnd = repIndex < prefixStartIndex ? dictEnd : iend;
  376. size_t const rLength = ZSTD_count_2segments(ip+1 +4, repMatch +4, iend, repMatchEnd, prefixStart) + 4;
  377. ip++;
  378. ZSTD_storeSeq(seqStore, (size_t)(ip-anchor), anchor, iend, 0, rLength-MINMATCH);
  379. ip += rLength;
  380. anchor = ip;
  381. } else {
  382. if ( (matchIndex < dictStartIndex) ||
  383. (MEM_read32(match) != MEM_read32(ip)) ) {
  384. assert(stepSize >= 1);
  385. ip += ((ip-anchor) >> kSearchStrength) + stepSize;
  386. continue;
  387. }
  388. { const BYTE* const matchEnd = matchIndex < prefixStartIndex ? dictEnd : iend;
  389. const BYTE* const lowMatchPtr = matchIndex < prefixStartIndex ? dictStart : prefixStart;
  390. U32 const offset = current - matchIndex;
  391. size_t mLength = ZSTD_count_2segments(ip+4, match+4, iend, matchEnd, prefixStart) + 4;
  392. while (((ip>anchor) & (match>lowMatchPtr)) && (ip[-1] == match[-1])) { ip--; match--; mLength++; } /* catch up */
  393. offset_2 = offset_1; offset_1 = offset; /* update offset history */
  394. ZSTD_storeSeq(seqStore, (size_t)(ip-anchor), anchor, iend, offset + ZSTD_REP_MOVE, mLength-MINMATCH);
  395. ip += mLength;
  396. anchor = ip;
  397. } }
  398. if (ip <= ilimit) {
  399. /* Fill Table */
  400. hashTable[ZSTD_hashPtr(base+current+2, hlog, mls)] = current+2;
  401. hashTable[ZSTD_hashPtr(ip-2, hlog, mls)] = (U32)(ip-2-base);
  402. /* check immediate repcode */
  403. while (ip <= ilimit) {
  404. U32 const current2 = (U32)(ip-base);
  405. U32 const repIndex2 = current2 - offset_2;
  406. const BYTE* const repMatch2 = repIndex2 < prefixStartIndex ? dictBase + repIndex2 : base + repIndex2;
  407. if ( (((U32)((prefixStartIndex-1) - repIndex2) >= 3) & (repIndex2 > dictStartIndex)) /* intentional overflow */
  408. && (MEM_read32(repMatch2) == MEM_read32(ip)) ) {
  409. const BYTE* const repEnd2 = repIndex2 < prefixStartIndex ? dictEnd : iend;
  410. size_t const repLength2 = ZSTD_count_2segments(ip+4, repMatch2+4, iend, repEnd2, prefixStart) + 4;
  411. { U32 const tmpOffset = offset_2; offset_2 = offset_1; offset_1 = tmpOffset; } /* swap offset_2 <=> offset_1 */
  412. ZSTD_storeSeq(seqStore, 0 /*litlen*/, anchor, iend, 0 /*offcode*/, repLength2-MINMATCH);
  413. hashTable[ZSTD_hashPtr(ip, hlog, mls)] = current2;
  414. ip += repLength2;
  415. anchor = ip;
  416. continue;
  417. }
  418. break;
  419. } } }
  420. /* save reps for next block */
  421. rep[0] = offset_1;
  422. rep[1] = offset_2;
  423. /* Return the last literals size */
  424. return (size_t)(iend - anchor);
  425. }
  426. size_t ZSTD_compressBlock_fast_extDict(
  427. ZSTD_matchState_t* ms, seqStore_t* seqStore, U32 rep[ZSTD_REP_NUM],
  428. void const* src, size_t srcSize)
  429. {
  430. U32 const mls = ms->cParams.minMatch;
  431. switch(mls)
  432. {
  433. default: /* includes case 3 */
  434. case 4 :
  435. return ZSTD_compressBlock_fast_extDict_generic(ms, seqStore, rep, src, srcSize, 4);
  436. case 5 :
  437. return ZSTD_compressBlock_fast_extDict_generic(ms, seqStore, rep, src, srcSize, 5);
  438. case 6 :
  439. return ZSTD_compressBlock_fast_extDict_generic(ms, seqStore, rep, src, srcSize, 6);
  440. case 7 :
  441. return ZSTD_compressBlock_fast_extDict_generic(ms, seqStore, rep, src, srcSize, 7);
  442. }
  443. }