zstd_double_fast.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519
  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"
  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. _match_found:
  224. offset_2 = offset_1;
  225. offset_1 = offset;
  226. ZSTD_storeSeq(seqStore, (size_t)(ip-anchor), anchor, iend, offset + ZSTD_REP_MOVE, mLength-MINMATCH);
  227. _match_stored:
  228. /* match found */
  229. ip += mLength;
  230. anchor = ip;
  231. if (ip <= ilimit) {
  232. /* Complementary insertion */
  233. /* done after iLimit test, as candidates could be > iend-8 */
  234. { U32 const indexToInsert = curr+2;
  235. hashLong[ZSTD_hashPtr(base+indexToInsert, hBitsL, 8)] = indexToInsert;
  236. hashLong[ZSTD_hashPtr(ip-2, hBitsL, 8)] = (U32)(ip-2-base);
  237. hashSmall[ZSTD_hashPtr(base+indexToInsert, hBitsS, mls)] = indexToInsert;
  238. hashSmall[ZSTD_hashPtr(ip-1, hBitsS, mls)] = (U32)(ip-1-base);
  239. }
  240. /* check immediate repcode */
  241. if (dictMode == ZSTD_dictMatchState) {
  242. while (ip <= ilimit) {
  243. U32 const current2 = (U32)(ip-base);
  244. U32 const repIndex2 = current2 - offset_2;
  245. const BYTE* repMatch2 = dictMode == ZSTD_dictMatchState
  246. && repIndex2 < prefixLowestIndex ?
  247. dictBase + repIndex2 - dictIndexDelta :
  248. base + repIndex2;
  249. if ( ((U32)((prefixLowestIndex-1) - (U32)repIndex2) >= 3 /* intentional overflow */)
  250. && (MEM_read32(repMatch2) == MEM_read32(ip)) ) {
  251. const BYTE* const repEnd2 = repIndex2 < prefixLowestIndex ? dictEnd : iend;
  252. size_t const repLength2 = ZSTD_count_2segments(ip+4, repMatch2+4, iend, repEnd2, prefixLowest) + 4;
  253. U32 tmpOffset = offset_2; offset_2 = offset_1; offset_1 = tmpOffset; /* swap offset_2 <=> offset_1 */
  254. ZSTD_storeSeq(seqStore, 0, anchor, iend, 0, repLength2-MINMATCH);
  255. hashSmall[ZSTD_hashPtr(ip, hBitsS, mls)] = current2;
  256. hashLong[ZSTD_hashPtr(ip, hBitsL, 8)] = current2;
  257. ip += repLength2;
  258. anchor = ip;
  259. continue;
  260. }
  261. break;
  262. } }
  263. if (dictMode == ZSTD_noDict) {
  264. while ( (ip <= ilimit)
  265. && ( (offset_2>0)
  266. & (MEM_read32(ip) == MEM_read32(ip - offset_2)) )) {
  267. /* store sequence */
  268. size_t const rLength = ZSTD_count(ip+4, ip+4-offset_2, iend) + 4;
  269. U32 const tmpOff = offset_2; offset_2 = offset_1; offset_1 = tmpOff; /* swap offset_2 <=> offset_1 */
  270. hashSmall[ZSTD_hashPtr(ip, hBitsS, mls)] = (U32)(ip-base);
  271. hashLong[ZSTD_hashPtr(ip, hBitsL, 8)] = (U32)(ip-base);
  272. ZSTD_storeSeq(seqStore, 0, anchor, iend, 0, rLength-MINMATCH);
  273. ip += rLength;
  274. anchor = ip;
  275. continue; /* faster when present ... (?) */
  276. } } }
  277. } /* while (ip < ilimit) */
  278. /* save reps for next block */
  279. rep[0] = offset_1 ? offset_1 : offsetSaved;
  280. rep[1] = offset_2 ? offset_2 : offsetSaved;
  281. /* Return the last literals size */
  282. return (size_t)(iend - anchor);
  283. }
  284. size_t ZSTD_compressBlock_doubleFast(
  285. ZSTD_matchState_t* ms, seqStore_t* seqStore, U32 rep[ZSTD_REP_NUM],
  286. void const* src, size_t srcSize)
  287. {
  288. const U32 mls = ms->cParams.minMatch;
  289. switch(mls)
  290. {
  291. default: /* includes case 3 */
  292. case 4 :
  293. return ZSTD_compressBlock_doubleFast_generic(ms, seqStore, rep, src, srcSize, 4, ZSTD_noDict);
  294. case 5 :
  295. return ZSTD_compressBlock_doubleFast_generic(ms, seqStore, rep, src, srcSize, 5, ZSTD_noDict);
  296. case 6 :
  297. return ZSTD_compressBlock_doubleFast_generic(ms, seqStore, rep, src, srcSize, 6, ZSTD_noDict);
  298. case 7 :
  299. return ZSTD_compressBlock_doubleFast_generic(ms, seqStore, rep, src, srcSize, 7, ZSTD_noDict);
  300. }
  301. }
  302. size_t ZSTD_compressBlock_doubleFast_dictMatchState(
  303. ZSTD_matchState_t* ms, seqStore_t* seqStore, U32 rep[ZSTD_REP_NUM],
  304. void const* src, size_t srcSize)
  305. {
  306. const U32 mls = ms->cParams.minMatch;
  307. switch(mls)
  308. {
  309. default: /* includes case 3 */
  310. case 4 :
  311. return ZSTD_compressBlock_doubleFast_generic(ms, seqStore, rep, src, srcSize, 4, ZSTD_dictMatchState);
  312. case 5 :
  313. return ZSTD_compressBlock_doubleFast_generic(ms, seqStore, rep, src, srcSize, 5, ZSTD_dictMatchState);
  314. case 6 :
  315. return ZSTD_compressBlock_doubleFast_generic(ms, seqStore, rep, src, srcSize, 6, ZSTD_dictMatchState);
  316. case 7 :
  317. return ZSTD_compressBlock_doubleFast_generic(ms, seqStore, rep, src, srcSize, 7, ZSTD_dictMatchState);
  318. }
  319. }
  320. static size_t ZSTD_compressBlock_doubleFast_extDict_generic(
  321. ZSTD_matchState_t* ms, seqStore_t* seqStore, U32 rep[ZSTD_REP_NUM],
  322. void const* src, size_t srcSize,
  323. U32 const mls /* template */)
  324. {
  325. ZSTD_compressionParameters const* cParams = &ms->cParams;
  326. U32* const hashLong = ms->hashTable;
  327. U32 const hBitsL = cParams->hashLog;
  328. U32* const hashSmall = ms->chainTable;
  329. U32 const hBitsS = cParams->chainLog;
  330. const BYTE* const istart = (const BYTE*)src;
  331. const BYTE* ip = istart;
  332. const BYTE* anchor = istart;
  333. const BYTE* const iend = istart + srcSize;
  334. const BYTE* const ilimit = iend - 8;
  335. const BYTE* const base = ms->window.base;
  336. const U32 endIndex = (U32)((size_t)(istart - base) + srcSize);
  337. const U32 lowLimit = ZSTD_getLowestMatchIndex(ms, endIndex, cParams->windowLog);
  338. const U32 dictStartIndex = lowLimit;
  339. const U32 dictLimit = ms->window.dictLimit;
  340. const U32 prefixStartIndex = (dictLimit > lowLimit) ? dictLimit : lowLimit;
  341. const BYTE* const prefixStart = base + prefixStartIndex;
  342. const BYTE* const dictBase = ms->window.dictBase;
  343. const BYTE* const dictStart = dictBase + dictStartIndex;
  344. const BYTE* const dictEnd = dictBase + prefixStartIndex;
  345. U32 offset_1=rep[0], offset_2=rep[1];
  346. DEBUGLOG(5, "ZSTD_compressBlock_doubleFast_extDict_generic (srcSize=%zu)", srcSize);
  347. /* if extDict is invalidated due to maxDistance, switch to "regular" variant */
  348. if (prefixStartIndex == dictStartIndex)
  349. return ZSTD_compressBlock_doubleFast_generic(ms, seqStore, rep, src, srcSize, mls, ZSTD_noDict);
  350. /* Search Loop */
  351. while (ip < ilimit) { /* < instead of <=, because (ip+1) */
  352. const size_t hSmall = ZSTD_hashPtr(ip, hBitsS, mls);
  353. const U32 matchIndex = hashSmall[hSmall];
  354. const BYTE* const matchBase = matchIndex < prefixStartIndex ? dictBase : base;
  355. const BYTE* match = matchBase + matchIndex;
  356. const size_t hLong = ZSTD_hashPtr(ip, hBitsL, 8);
  357. const U32 matchLongIndex = hashLong[hLong];
  358. const BYTE* const matchLongBase = matchLongIndex < prefixStartIndex ? dictBase : base;
  359. const BYTE* matchLong = matchLongBase + matchLongIndex;
  360. const U32 curr = (U32)(ip-base);
  361. const U32 repIndex = curr + 1 - offset_1; /* offset_1 expected <= curr +1 */
  362. const BYTE* const repBase = repIndex < prefixStartIndex ? dictBase : base;
  363. const BYTE* const repMatch = repBase + repIndex;
  364. size_t mLength;
  365. hashSmall[hSmall] = hashLong[hLong] = curr; /* update hash table */
  366. if ((((U32)((prefixStartIndex-1) - repIndex) >= 3) /* intentional underflow : ensure repIndex doesn't overlap dict + prefix */
  367. & (repIndex > dictStartIndex))
  368. && (MEM_read32(repMatch) == MEM_read32(ip+1)) ) {
  369. const BYTE* repMatchEnd = repIndex < prefixStartIndex ? dictEnd : iend;
  370. mLength = ZSTD_count_2segments(ip+1+4, repMatch+4, iend, repMatchEnd, prefixStart) + 4;
  371. ip++;
  372. ZSTD_storeSeq(seqStore, (size_t)(ip-anchor), anchor, iend, 0, mLength-MINMATCH);
  373. } else {
  374. if ((matchLongIndex > dictStartIndex) && (MEM_read64(matchLong) == MEM_read64(ip))) {
  375. const BYTE* const matchEnd = matchLongIndex < prefixStartIndex ? dictEnd : iend;
  376. const BYTE* const lowMatchPtr = matchLongIndex < prefixStartIndex ? dictStart : prefixStart;
  377. U32 offset;
  378. mLength = ZSTD_count_2segments(ip+8, matchLong+8, iend, matchEnd, prefixStart) + 8;
  379. offset = curr - matchLongIndex;
  380. while (((ip>anchor) & (matchLong>lowMatchPtr)) && (ip[-1] == matchLong[-1])) { ip--; matchLong--; mLength++; } /* catch up */
  381. offset_2 = offset_1;
  382. offset_1 = offset;
  383. ZSTD_storeSeq(seqStore, (size_t)(ip-anchor), anchor, iend, offset + ZSTD_REP_MOVE, mLength-MINMATCH);
  384. } else if ((matchIndex > dictStartIndex) && (MEM_read32(match) == MEM_read32(ip))) {
  385. size_t const h3 = ZSTD_hashPtr(ip+1, hBitsL, 8);
  386. U32 const matchIndex3 = hashLong[h3];
  387. const BYTE* const match3Base = matchIndex3 < prefixStartIndex ? dictBase : base;
  388. const BYTE* match3 = match3Base + matchIndex3;
  389. U32 offset;
  390. hashLong[h3] = curr + 1;
  391. if ( (matchIndex3 > dictStartIndex) && (MEM_read64(match3) == MEM_read64(ip+1)) ) {
  392. const BYTE* const matchEnd = matchIndex3 < prefixStartIndex ? dictEnd : iend;
  393. const BYTE* const lowMatchPtr = matchIndex3 < prefixStartIndex ? dictStart : prefixStart;
  394. mLength = ZSTD_count_2segments(ip+9, match3+8, iend, matchEnd, prefixStart) + 8;
  395. ip++;
  396. offset = curr+1 - matchIndex3;
  397. while (((ip>anchor) & (match3>lowMatchPtr)) && (ip[-1] == match3[-1])) { ip--; match3--; mLength++; } /* catch up */
  398. } else {
  399. const BYTE* const matchEnd = matchIndex < prefixStartIndex ? dictEnd : iend;
  400. const BYTE* const lowMatchPtr = matchIndex < prefixStartIndex ? dictStart : prefixStart;
  401. mLength = ZSTD_count_2segments(ip+4, match+4, iend, matchEnd, prefixStart) + 4;
  402. offset = curr - matchIndex;
  403. while (((ip>anchor) & (match>lowMatchPtr)) && (ip[-1] == match[-1])) { ip--; match--; mLength++; } /* catch up */
  404. }
  405. offset_2 = offset_1;
  406. offset_1 = offset;
  407. ZSTD_storeSeq(seqStore, (size_t)(ip-anchor), anchor, iend, offset + ZSTD_REP_MOVE, mLength-MINMATCH);
  408. } else {
  409. ip += ((ip-anchor) >> kSearchStrength) + 1;
  410. continue;
  411. } }
  412. /* move to next sequence start */
  413. ip += mLength;
  414. anchor = ip;
  415. if (ip <= ilimit) {
  416. /* Complementary insertion */
  417. /* done after iLimit test, as candidates could be > iend-8 */
  418. { U32 const indexToInsert = curr+2;
  419. hashLong[ZSTD_hashPtr(base+indexToInsert, hBitsL, 8)] = indexToInsert;
  420. hashLong[ZSTD_hashPtr(ip-2, hBitsL, 8)] = (U32)(ip-2-base);
  421. hashSmall[ZSTD_hashPtr(base+indexToInsert, hBitsS, mls)] = indexToInsert;
  422. hashSmall[ZSTD_hashPtr(ip-1, hBitsS, mls)] = (U32)(ip-1-base);
  423. }
  424. /* check immediate repcode */
  425. while (ip <= ilimit) {
  426. U32 const current2 = (U32)(ip-base);
  427. U32 const repIndex2 = current2 - offset_2;
  428. const BYTE* repMatch2 = repIndex2 < prefixStartIndex ? dictBase + repIndex2 : base + repIndex2;
  429. if ( (((U32)((prefixStartIndex-1) - repIndex2) >= 3) /* intentional overflow : ensure repIndex2 doesn't overlap dict + prefix */
  430. & (repIndex2 > dictStartIndex))
  431. && (MEM_read32(repMatch2) == MEM_read32(ip)) ) {
  432. const BYTE* const repEnd2 = repIndex2 < prefixStartIndex ? dictEnd : iend;
  433. size_t const repLength2 = ZSTD_count_2segments(ip+4, repMatch2+4, iend, repEnd2, prefixStart) + 4;
  434. U32 const tmpOffset = offset_2; offset_2 = offset_1; offset_1 = tmpOffset; /* swap offset_2 <=> offset_1 */
  435. ZSTD_storeSeq(seqStore, 0, anchor, iend, 0, repLength2-MINMATCH);
  436. hashSmall[ZSTD_hashPtr(ip, hBitsS, mls)] = current2;
  437. hashLong[ZSTD_hashPtr(ip, hBitsL, 8)] = current2;
  438. ip += repLength2;
  439. anchor = ip;
  440. continue;
  441. }
  442. break;
  443. } } }
  444. /* save reps for next block */
  445. rep[0] = offset_1;
  446. rep[1] = offset_2;
  447. /* Return the last literals size */
  448. return (size_t)(iend - anchor);
  449. }
  450. size_t ZSTD_compressBlock_doubleFast_extDict(
  451. ZSTD_matchState_t* ms, seqStore_t* seqStore, U32 rep[ZSTD_REP_NUM],
  452. void const* src, size_t srcSize)
  453. {
  454. U32 const mls = ms->cParams.minMatch;
  455. switch(mls)
  456. {
  457. default: /* includes case 3 */
  458. case 4 :
  459. return ZSTD_compressBlock_doubleFast_extDict_generic(ms, seqStore, rep, src, srcSize, 4);
  460. case 5 :
  461. return ZSTD_compressBlock_doubleFast_extDict_generic(ms, seqStore, rep, src, srcSize, 5);
  462. case 6 :
  463. return ZSTD_compressBlock_doubleFast_extDict_generic(ms, seqStore, rep, src, srcSize, 6);
  464. case 7 :
  465. return ZSTD_compressBlock_doubleFast_extDict_generic(ms, seqStore, rep, src, srcSize, 7);
  466. }
  467. }