zstd_double_fast.c 25 KB

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