zstd_double_fast.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500
  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"
  11. #include "zstd_double_fast.h"
  12. void ZSTD_fillDoubleHashTable(ZSTD_matchState_t* ms,
  13. void const* end, ZSTD_dictTableLoadMethod_e dtlm)
  14. {
  15. const ZSTD_compressionParameters* const cParams = &ms->cParams;
  16. U32* const hashLarge = ms->hashTable;
  17. U32 const hBitsL = cParams->hashLog;
  18. U32 const mls = cParams->minMatch;
  19. U32* const hashSmall = ms->chainTable;
  20. U32 const hBitsS = cParams->chainLog;
  21. const BYTE* const base = ms->window.base;
  22. const BYTE* ip = base + ms->nextToUpdate;
  23. const BYTE* const iend = ((const BYTE*)end) - HASH_READ_SIZE;
  24. const U32 fastHashFillStep = 3;
  25. /* Always insert every fastHashFillStep position into the hash tables.
  26. * Insert the other positions into the large hash table if their entry
  27. * is empty.
  28. */
  29. for (; ip + fastHashFillStep - 1 <= iend; ip += fastHashFillStep) {
  30. U32 const current = (U32)(ip - base);
  31. U32 i;
  32. for (i = 0; i < fastHashFillStep; ++i) {
  33. size_t const smHash = ZSTD_hashPtr(ip + i, hBitsS, mls);
  34. size_t const lgHash = ZSTD_hashPtr(ip + i, hBitsL, 8);
  35. if (i == 0)
  36. hashSmall[smHash] = current + i;
  37. if (i == 0 || hashLarge[lgHash] == 0)
  38. hashLarge[lgHash] = current + i;
  39. /* Only load extra positions for ZSTD_dtlm_full */
  40. if (dtlm == ZSTD_dtlm_fast)
  41. break;
  42. }
  43. }
  44. }
  45. FORCE_INLINE_TEMPLATE
  46. size_t ZSTD_compressBlock_doubleFast_generic(
  47. ZSTD_matchState_t* ms, seqStore_t* seqStore, U32 rep[ZSTD_REP_NUM],
  48. void const* src, size_t srcSize,
  49. U32 const mls /* template */, ZSTD_dictMode_e const dictMode)
  50. {
  51. ZSTD_compressionParameters const* cParams = &ms->cParams;
  52. U32* const hashLong = ms->hashTable;
  53. const U32 hBitsL = cParams->hashLog;
  54. U32* const hashSmall = ms->chainTable;
  55. const U32 hBitsS = cParams->chainLog;
  56. const BYTE* const base = ms->window.base;
  57. const BYTE* const istart = (const BYTE*)src;
  58. const BYTE* ip = istart;
  59. const BYTE* anchor = istart;
  60. const U32 prefixLowestIndex = ms->window.dictLimit;
  61. const BYTE* const prefixLowest = base + prefixLowestIndex;
  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. const ZSTD_matchState_t* const dms = ms->dictMatchState;
  67. const ZSTD_compressionParameters* const dictCParams =
  68. dictMode == ZSTD_dictMatchState ?
  69. &dms->cParams : NULL;
  70. const U32* const dictHashLong = dictMode == ZSTD_dictMatchState ?
  71. dms->hashTable : NULL;
  72. const U32* const dictHashSmall = dictMode == ZSTD_dictMatchState ?
  73. dms->chainTable : NULL;
  74. const U32 dictStartIndex = dictMode == ZSTD_dictMatchState ?
  75. dms->window.dictLimit : 0;
  76. const BYTE* const dictBase = dictMode == ZSTD_dictMatchState ?
  77. dms->window.base : NULL;
  78. const BYTE* const dictStart = dictMode == ZSTD_dictMatchState ?
  79. dictBase + dictStartIndex : NULL;
  80. const BYTE* const dictEnd = dictMode == ZSTD_dictMatchState ?
  81. dms->window.nextSrc : NULL;
  82. const U32 dictIndexDelta = dictMode == ZSTD_dictMatchState ?
  83. prefixLowestIndex - (U32)(dictEnd - dictBase) :
  84. 0;
  85. const U32 dictHBitsL = dictMode == ZSTD_dictMatchState ?
  86. dictCParams->hashLog : hBitsL;
  87. const U32 dictHBitsS = dictMode == ZSTD_dictMatchState ?
  88. dictCParams->chainLog : hBitsS;
  89. const U32 dictAndPrefixLength = (U32)(ip - prefixLowest + dictEnd - dictStart);
  90. assert(dictMode == ZSTD_noDict || dictMode == ZSTD_dictMatchState);
  91. /* init */
  92. ip += (dictAndPrefixLength == 0);
  93. if (dictMode == ZSTD_noDict) {
  94. U32 const maxRep = (U32)(ip - prefixLowest);
  95. if (offset_2 > maxRep) offsetSaved = offset_2, offset_2 = 0;
  96. if (offset_1 > maxRep) offsetSaved = offset_1, offset_1 = 0;
  97. }
  98. if (dictMode == ZSTD_dictMatchState) {
  99. /* dictMatchState repCode checks don't currently handle repCode == 0
  100. * disabling. */
  101. assert(offset_1 <= dictAndPrefixLength);
  102. assert(offset_2 <= dictAndPrefixLength);
  103. }
  104. /* Main Search Loop */
  105. while (ip < ilimit) { /* < instead of <=, because repcode check at (ip+1) */
  106. size_t mLength;
  107. U32 offset;
  108. size_t const h2 = ZSTD_hashPtr(ip, hBitsL, 8);
  109. size_t const h = ZSTD_hashPtr(ip, hBitsS, mls);
  110. size_t const dictHL = ZSTD_hashPtr(ip, dictHBitsL, 8);
  111. size_t const dictHS = ZSTD_hashPtr(ip, dictHBitsS, mls);
  112. U32 const current = (U32)(ip-base);
  113. U32 const matchIndexL = hashLong[h2];
  114. U32 matchIndexS = hashSmall[h];
  115. const BYTE* matchLong = base + matchIndexL;
  116. const BYTE* match = base + matchIndexS;
  117. const U32 repIndex = current + 1 - offset_1;
  118. const BYTE* repMatch = (dictMode == ZSTD_dictMatchState
  119. && repIndex < prefixLowestIndex) ?
  120. dictBase + (repIndex - dictIndexDelta) :
  121. base + repIndex;
  122. hashLong[h2] = hashSmall[h] = current; /* update hash tables */
  123. /* check dictMatchState repcode */
  124. if (dictMode == ZSTD_dictMatchState
  125. && ((U32)((prefixLowestIndex-1) - repIndex) >= 3 /* intentional underflow */)
  126. && (MEM_read32(repMatch) == MEM_read32(ip+1)) ) {
  127. const BYTE* repMatchEnd = repIndex < prefixLowestIndex ? dictEnd : iend;
  128. mLength = ZSTD_count_2segments(ip+1+4, repMatch+4, iend, repMatchEnd, prefixLowest) + 4;
  129. ip++;
  130. ZSTD_storeSeq(seqStore, ip-anchor, anchor, 0, mLength-MINMATCH);
  131. goto _match_stored;
  132. }
  133. /* check noDict repcode */
  134. if ( dictMode == ZSTD_noDict
  135. && ((offset_1 > 0) & (MEM_read32(ip+1-offset_1) == MEM_read32(ip+1)))) {
  136. mLength = ZSTD_count(ip+1+4, ip+1+4-offset_1, iend) + 4;
  137. ip++;
  138. ZSTD_storeSeq(seqStore, ip-anchor, anchor, 0, mLength-MINMATCH);
  139. goto _match_stored;
  140. }
  141. if (matchIndexL > prefixLowestIndex) {
  142. /* check prefix long match */
  143. if (MEM_read64(matchLong) == MEM_read64(ip)) {
  144. mLength = ZSTD_count(ip+8, matchLong+8, iend) + 8;
  145. offset = (U32)(ip-matchLong);
  146. while (((ip>anchor) & (matchLong>prefixLowest)) && (ip[-1] == matchLong[-1])) { ip--; matchLong--; mLength++; } /* catch up */
  147. goto _match_found;
  148. }
  149. } else if (dictMode == ZSTD_dictMatchState) {
  150. /* check dictMatchState long match */
  151. U32 const dictMatchIndexL = dictHashLong[dictHL];
  152. const BYTE* dictMatchL = dictBase + dictMatchIndexL;
  153. assert(dictMatchL < dictEnd);
  154. if (dictMatchL > dictStart && MEM_read64(dictMatchL) == MEM_read64(ip)) {
  155. mLength = ZSTD_count_2segments(ip+8, dictMatchL+8, iend, dictEnd, prefixLowest) + 8;
  156. offset = (U32)(current - dictMatchIndexL - dictIndexDelta);
  157. while (((ip>anchor) & (dictMatchL>dictStart)) && (ip[-1] == dictMatchL[-1])) { ip--; dictMatchL--; mLength++; } /* catch up */
  158. goto _match_found;
  159. }
  160. }
  161. if (matchIndexS > prefixLowestIndex) {
  162. /* check prefix short match */
  163. if (MEM_read32(match) == MEM_read32(ip)) {
  164. goto _search_next_long;
  165. }
  166. } else if (dictMode == ZSTD_dictMatchState) {
  167. /* check dictMatchState short match */
  168. U32 const dictMatchIndexS = dictHashSmall[dictHS];
  169. match = dictBase + dictMatchIndexS;
  170. matchIndexS = dictMatchIndexS + dictIndexDelta;
  171. if (match > dictStart && MEM_read32(match) == MEM_read32(ip)) {
  172. goto _search_next_long;
  173. }
  174. }
  175. ip += ((ip-anchor) >> kSearchStrength) + 1;
  176. continue;
  177. _search_next_long:
  178. {
  179. size_t const hl3 = ZSTD_hashPtr(ip+1, hBitsL, 8);
  180. size_t const dictHLNext = ZSTD_hashPtr(ip+1, dictHBitsL, 8);
  181. U32 const matchIndexL3 = hashLong[hl3];
  182. const BYTE* matchL3 = base + matchIndexL3;
  183. hashLong[hl3] = current + 1;
  184. /* check prefix long +1 match */
  185. if (matchIndexL3 > prefixLowestIndex) {
  186. if (MEM_read64(matchL3) == MEM_read64(ip+1)) {
  187. mLength = ZSTD_count(ip+9, matchL3+8, iend) + 8;
  188. ip++;
  189. offset = (U32)(ip-matchL3);
  190. while (((ip>anchor) & (matchL3>prefixLowest)) && (ip[-1] == matchL3[-1])) { ip--; matchL3--; mLength++; } /* catch up */
  191. goto _match_found;
  192. }
  193. } else if (dictMode == ZSTD_dictMatchState) {
  194. /* check dict long +1 match */
  195. U32 const dictMatchIndexL3 = dictHashLong[dictHLNext];
  196. const BYTE* dictMatchL3 = dictBase + dictMatchIndexL3;
  197. assert(dictMatchL3 < dictEnd);
  198. if (dictMatchL3 > dictStart && MEM_read64(dictMatchL3) == MEM_read64(ip+1)) {
  199. mLength = ZSTD_count_2segments(ip+1+8, dictMatchL3+8, iend, dictEnd, prefixLowest) + 8;
  200. ip++;
  201. offset = (U32)(current + 1 - dictMatchIndexL3 - dictIndexDelta);
  202. while (((ip>anchor) & (dictMatchL3>dictStart)) && (ip[-1] == dictMatchL3[-1])) { ip--; dictMatchL3--; mLength++; } /* catch up */
  203. goto _match_found;
  204. }
  205. }
  206. }
  207. /* if no long +1 match, explore the short match we found */
  208. if (dictMode == ZSTD_dictMatchState && matchIndexS < prefixLowestIndex) {
  209. mLength = ZSTD_count_2segments(ip+4, match+4, iend, dictEnd, prefixLowest) + 4;
  210. offset = (U32)(current - matchIndexS);
  211. while (((ip>anchor) & (match>dictStart)) && (ip[-1] == match[-1])) { ip--; match--; mLength++; } /* catch up */
  212. } else {
  213. mLength = ZSTD_count(ip+4, match+4, iend) + 4;
  214. offset = (U32)(ip - match);
  215. while (((ip>anchor) & (match>prefixLowest)) && (ip[-1] == match[-1])) { ip--; match--; mLength++; } /* catch up */
  216. }
  217. /* fall-through */
  218. _match_found:
  219. offset_2 = offset_1;
  220. offset_1 = offset;
  221. ZSTD_storeSeq(seqStore, ip-anchor, anchor, offset + ZSTD_REP_MOVE, mLength-MINMATCH);
  222. _match_stored:
  223. /* match found */
  224. ip += mLength;
  225. anchor = ip;
  226. if (ip <= ilimit) {
  227. /* Fill Table */
  228. hashLong[ZSTD_hashPtr(base+current+2, hBitsL, 8)] =
  229. hashSmall[ZSTD_hashPtr(base+current+2, hBitsS, mls)] = current+2; /* here because current+2 could be > iend-8 */
  230. hashLong[ZSTD_hashPtr(ip-2, hBitsL, 8)] =
  231. hashSmall[ZSTD_hashPtr(ip-2, hBitsS, mls)] = (U32)(ip-2-base);
  232. /* check immediate repcode */
  233. if (dictMode == ZSTD_dictMatchState) {
  234. while (ip <= ilimit) {
  235. U32 const current2 = (U32)(ip-base);
  236. U32 const repIndex2 = current2 - offset_2;
  237. const BYTE* repMatch2 = dictMode == ZSTD_dictMatchState
  238. && repIndex2 < prefixLowestIndex ?
  239. dictBase - dictIndexDelta + repIndex2 :
  240. base + repIndex2;
  241. if ( ((U32)((prefixLowestIndex-1) - (U32)repIndex2) >= 3 /* intentional overflow */)
  242. && (MEM_read32(repMatch2) == MEM_read32(ip)) ) {
  243. const BYTE* const repEnd2 = repIndex2 < prefixLowestIndex ? dictEnd : iend;
  244. size_t const repLength2 = ZSTD_count_2segments(ip+4, repMatch2+4, iend, repEnd2, prefixLowest) + 4;
  245. U32 tmpOffset = offset_2; offset_2 = offset_1; offset_1 = tmpOffset; /* swap offset_2 <=> offset_1 */
  246. ZSTD_storeSeq(seqStore, 0, anchor, 0, repLength2-MINMATCH);
  247. hashSmall[ZSTD_hashPtr(ip, hBitsS, mls)] = current2;
  248. hashLong[ZSTD_hashPtr(ip, hBitsL, 8)] = current2;
  249. ip += repLength2;
  250. anchor = ip;
  251. continue;
  252. }
  253. break;
  254. }
  255. }
  256. if (dictMode == ZSTD_noDict) {
  257. while ( (ip <= ilimit)
  258. && ( (offset_2>0)
  259. & (MEM_read32(ip) == MEM_read32(ip - offset_2)) )) {
  260. /* store sequence */
  261. size_t const rLength = ZSTD_count(ip+4, ip+4-offset_2, iend) + 4;
  262. U32 const tmpOff = offset_2; offset_2 = offset_1; offset_1 = tmpOff; /* swap offset_2 <=> offset_1 */
  263. hashSmall[ZSTD_hashPtr(ip, hBitsS, mls)] = (U32)(ip-base);
  264. hashLong[ZSTD_hashPtr(ip, hBitsL, 8)] = (U32)(ip-base);
  265. ZSTD_storeSeq(seqStore, 0, anchor, 0, rLength-MINMATCH);
  266. ip += rLength;
  267. anchor = ip;
  268. continue; /* faster when present ... (?) */
  269. } } } }
  270. /* save reps for next block */
  271. rep[0] = offset_1 ? offset_1 : offsetSaved;
  272. rep[1] = offset_2 ? offset_2 : offsetSaved;
  273. /* Return the last literals size */
  274. return iend - anchor;
  275. }
  276. size_t ZSTD_compressBlock_doubleFast(
  277. ZSTD_matchState_t* ms, seqStore_t* seqStore, U32 rep[ZSTD_REP_NUM],
  278. void const* src, size_t srcSize)
  279. {
  280. const U32 mls = ms->cParams.minMatch;
  281. switch(mls)
  282. {
  283. default: /* includes case 3 */
  284. case 4 :
  285. return ZSTD_compressBlock_doubleFast_generic(ms, seqStore, rep, src, srcSize, 4, ZSTD_noDict);
  286. case 5 :
  287. return ZSTD_compressBlock_doubleFast_generic(ms, seqStore, rep, src, srcSize, 5, ZSTD_noDict);
  288. case 6 :
  289. return ZSTD_compressBlock_doubleFast_generic(ms, seqStore, rep, src, srcSize, 6, ZSTD_noDict);
  290. case 7 :
  291. return ZSTD_compressBlock_doubleFast_generic(ms, seqStore, rep, src, srcSize, 7, ZSTD_noDict);
  292. }
  293. }
  294. size_t ZSTD_compressBlock_doubleFast_dictMatchState(
  295. ZSTD_matchState_t* ms, seqStore_t* seqStore, U32 rep[ZSTD_REP_NUM],
  296. void const* src, size_t srcSize)
  297. {
  298. const U32 mls = ms->cParams.minMatch;
  299. switch(mls)
  300. {
  301. default: /* includes case 3 */
  302. case 4 :
  303. return ZSTD_compressBlock_doubleFast_generic(ms, seqStore, rep, src, srcSize, 4, ZSTD_dictMatchState);
  304. case 5 :
  305. return ZSTD_compressBlock_doubleFast_generic(ms, seqStore, rep, src, srcSize, 5, ZSTD_dictMatchState);
  306. case 6 :
  307. return ZSTD_compressBlock_doubleFast_generic(ms, seqStore, rep, src, srcSize, 6, ZSTD_dictMatchState);
  308. case 7 :
  309. return ZSTD_compressBlock_doubleFast_generic(ms, seqStore, rep, src, srcSize, 7, ZSTD_dictMatchState);
  310. }
  311. }
  312. static size_t ZSTD_compressBlock_doubleFast_extDict_generic(
  313. ZSTD_matchState_t* ms, seqStore_t* seqStore, U32 rep[ZSTD_REP_NUM],
  314. void const* src, size_t srcSize,
  315. U32 const mls /* template */)
  316. {
  317. ZSTD_compressionParameters const* cParams = &ms->cParams;
  318. U32* const hashLong = ms->hashTable;
  319. U32 const hBitsL = cParams->hashLog;
  320. U32* const hashSmall = ms->chainTable;
  321. U32 const hBitsS = cParams->chainLog;
  322. const BYTE* const istart = (const BYTE*)src;
  323. const BYTE* ip = istart;
  324. const BYTE* anchor = istart;
  325. const BYTE* const iend = istart + srcSize;
  326. const BYTE* const ilimit = iend - 8;
  327. const U32 prefixStartIndex = ms->window.dictLimit;
  328. const BYTE* const base = ms->window.base;
  329. const BYTE* const prefixStart = base + prefixStartIndex;
  330. const U32 dictStartIndex = ms->window.lowLimit;
  331. const BYTE* const dictBase = ms->window.dictBase;
  332. const BYTE* const dictStart = dictBase + dictStartIndex;
  333. const BYTE* const dictEnd = dictBase + prefixStartIndex;
  334. U32 offset_1=rep[0], offset_2=rep[1];
  335. DEBUGLOG(5, "ZSTD_compressBlock_doubleFast_extDict_generic (srcSize=%zu)", srcSize);
  336. /* Search Loop */
  337. while (ip < ilimit) { /* < instead of <=, because (ip+1) */
  338. const size_t hSmall = ZSTD_hashPtr(ip, hBitsS, mls);
  339. const U32 matchIndex = hashSmall[hSmall];
  340. const BYTE* const matchBase = matchIndex < prefixStartIndex ? dictBase : base;
  341. const BYTE* match = matchBase + matchIndex;
  342. const size_t hLong = ZSTD_hashPtr(ip, hBitsL, 8);
  343. const U32 matchLongIndex = hashLong[hLong];
  344. const BYTE* const matchLongBase = matchLongIndex < prefixStartIndex ? dictBase : base;
  345. const BYTE* matchLong = matchLongBase + matchLongIndex;
  346. const U32 current = (U32)(ip-base);
  347. const U32 repIndex = current + 1 - offset_1; /* offset_1 expected <= current +1 */
  348. const BYTE* const repBase = repIndex < prefixStartIndex ? dictBase : base;
  349. const BYTE* const repMatch = repBase + repIndex;
  350. size_t mLength;
  351. hashSmall[hSmall] = hashLong[hLong] = current; /* update hash table */
  352. if ((((U32)((prefixStartIndex-1) - repIndex) >= 3) /* intentional underflow : ensure repIndex doesn't overlap dict + prefix */
  353. & (repIndex > dictStartIndex))
  354. && (MEM_read32(repMatch) == MEM_read32(ip+1)) ) {
  355. const BYTE* repMatchEnd = repIndex < prefixStartIndex ? dictEnd : iend;
  356. mLength = ZSTD_count_2segments(ip+1+4, repMatch+4, iend, repMatchEnd, prefixStart) + 4;
  357. ip++;
  358. ZSTD_storeSeq(seqStore, ip-anchor, anchor, 0, mLength-MINMATCH);
  359. } else {
  360. if ((matchLongIndex > dictStartIndex) && (MEM_read64(matchLong) == MEM_read64(ip))) {
  361. const BYTE* const matchEnd = matchLongIndex < prefixStartIndex ? dictEnd : iend;
  362. const BYTE* const lowMatchPtr = matchLongIndex < prefixStartIndex ? dictStart : prefixStart;
  363. U32 offset;
  364. mLength = ZSTD_count_2segments(ip+8, matchLong+8, iend, matchEnd, prefixStart) + 8;
  365. offset = current - matchLongIndex;
  366. while (((ip>anchor) & (matchLong>lowMatchPtr)) && (ip[-1] == matchLong[-1])) { ip--; matchLong--; mLength++; } /* catch up */
  367. offset_2 = offset_1;
  368. offset_1 = offset;
  369. ZSTD_storeSeq(seqStore, ip-anchor, anchor, offset + ZSTD_REP_MOVE, mLength-MINMATCH);
  370. } else if ((matchIndex > dictStartIndex) && (MEM_read32(match) == MEM_read32(ip))) {
  371. size_t const h3 = ZSTD_hashPtr(ip+1, hBitsL, 8);
  372. U32 const matchIndex3 = hashLong[h3];
  373. const BYTE* const match3Base = matchIndex3 < prefixStartIndex ? dictBase : base;
  374. const BYTE* match3 = match3Base + matchIndex3;
  375. U32 offset;
  376. hashLong[h3] = current + 1;
  377. if ( (matchIndex3 > dictStartIndex) && (MEM_read64(match3) == MEM_read64(ip+1)) ) {
  378. const BYTE* const matchEnd = matchIndex3 < prefixStartIndex ? dictEnd : iend;
  379. const BYTE* const lowMatchPtr = matchIndex3 < prefixStartIndex ? dictStart : prefixStart;
  380. mLength = ZSTD_count_2segments(ip+9, match3+8, iend, matchEnd, prefixStart) + 8;
  381. ip++;
  382. offset = current+1 - matchIndex3;
  383. while (((ip>anchor) & (match3>lowMatchPtr)) && (ip[-1] == match3[-1])) { ip--; match3--; mLength++; } /* catch up */
  384. } else {
  385. const BYTE* const matchEnd = matchIndex < prefixStartIndex ? dictEnd : iend;
  386. const BYTE* const lowMatchPtr = matchIndex < prefixStartIndex ? dictStart : prefixStart;
  387. mLength = ZSTD_count_2segments(ip+4, match+4, iend, matchEnd, prefixStart) + 4;
  388. offset = current - matchIndex;
  389. while (((ip>anchor) & (match>lowMatchPtr)) && (ip[-1] == match[-1])) { ip--; match--; mLength++; } /* catch up */
  390. }
  391. offset_2 = offset_1;
  392. offset_1 = offset;
  393. ZSTD_storeSeq(seqStore, ip-anchor, anchor, offset + ZSTD_REP_MOVE, mLength-MINMATCH);
  394. } else {
  395. ip += ((ip-anchor) >> kSearchStrength) + 1;
  396. continue;
  397. } }
  398. /* found a match : store it */
  399. ip += mLength;
  400. anchor = ip;
  401. if (ip <= ilimit) {
  402. /* Fill Table */
  403. hashSmall[ZSTD_hashPtr(base+current+2, hBitsS, mls)] = current+2;
  404. hashLong[ZSTD_hashPtr(base+current+2, hBitsL, 8)] = current+2;
  405. hashSmall[ZSTD_hashPtr(ip-2, hBitsS, mls)] = (U32)(ip-2-base);
  406. hashLong[ZSTD_hashPtr(ip-2, hBitsL, 8)] = (U32)(ip-2-base);
  407. /* check immediate repcode */
  408. while (ip <= ilimit) {
  409. U32 const current2 = (U32)(ip-base);
  410. U32 const repIndex2 = current2 - offset_2;
  411. const BYTE* repMatch2 = repIndex2 < prefixStartIndex ? dictBase + repIndex2 : base + repIndex2;
  412. if ( (((U32)((prefixStartIndex-1) - repIndex2) >= 3) /* intentional overflow : ensure repIndex2 doesn't overlap dict + prefix */
  413. & (repIndex2 > dictStartIndex))
  414. && (MEM_read32(repMatch2) == MEM_read32(ip)) ) {
  415. const BYTE* const repEnd2 = repIndex2 < prefixStartIndex ? dictEnd : iend;
  416. size_t const repLength2 = ZSTD_count_2segments(ip+4, repMatch2+4, iend, repEnd2, prefixStart) + 4;
  417. U32 const tmpOffset = offset_2; offset_2 = offset_1; offset_1 = tmpOffset; /* swap offset_2 <=> offset_1 */
  418. ZSTD_storeSeq(seqStore, 0, anchor, 0, repLength2-MINMATCH);
  419. hashSmall[ZSTD_hashPtr(ip, hBitsS, mls)] = current2;
  420. hashLong[ZSTD_hashPtr(ip, hBitsL, 8)] = current2;
  421. ip += repLength2;
  422. anchor = ip;
  423. continue;
  424. }
  425. break;
  426. } } }
  427. /* save reps for next block */
  428. rep[0] = offset_1;
  429. rep[1] = offset_2;
  430. /* Return the last literals size */
  431. return iend - anchor;
  432. }
  433. size_t ZSTD_compressBlock_doubleFast_extDict(
  434. ZSTD_matchState_t* ms, seqStore_t* seqStore, U32 rep[ZSTD_REP_NUM],
  435. void const* src, size_t srcSize)
  436. {
  437. U32 const mls = ms->cParams.minMatch;
  438. switch(mls)
  439. {
  440. default: /* includes case 3 */
  441. case 4 :
  442. return ZSTD_compressBlock_doubleFast_extDict_generic(ms, seqStore, rep, src, srcSize, 4);
  443. case 5 :
  444. return ZSTD_compressBlock_doubleFast_extDict_generic(ms, seqStore, rep, src, srcSize, 5);
  445. case 6 :
  446. return ZSTD_compressBlock_doubleFast_extDict_generic(ms, seqStore, rep, src, srcSize, 6);
  447. case 7 :
  448. return ZSTD_compressBlock_doubleFast_extDict_generic(ms, seqStore, rep, src, srcSize, 7);
  449. }
  450. }