zstd_double_fast.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758
  1. /*
  2. * Copyright (c) Meta Platforms, Inc. and affiliates.
  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. static void ZSTD_fillDoubleHashTableForCDict(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 + ZSTD_SHORT_CACHE_TAG_BITS;
  18. U32 const mls = cParams->minMatch;
  19. U32* const hashSmall = ms->chainTable;
  20. U32 const hBitsS = cParams->chainLog + ZSTD_SHORT_CACHE_TAG_BITS;
  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 smHashAndTag = ZSTD_hashPtr(ip + i, hBitsS, mls);
  34. size_t const lgHashAndTag = ZSTD_hashPtr(ip + i, hBitsL, 8);
  35. if (i == 0) {
  36. ZSTD_writeTaggedIndex(hashSmall, smHashAndTag, curr + i);
  37. }
  38. if (i == 0 || hashLarge[lgHashAndTag >> ZSTD_SHORT_CACHE_TAG_BITS] == 0) {
  39. ZSTD_writeTaggedIndex(hashLarge, lgHashAndTag, curr + i);
  40. }
  41. /* Only load extra positions for ZSTD_dtlm_full */
  42. if (dtlm == ZSTD_dtlm_fast)
  43. break;
  44. } }
  45. }
  46. static void ZSTD_fillDoubleHashTableForCCtx(ZSTD_matchState_t* ms,
  47. void const* end, ZSTD_dictTableLoadMethod_e dtlm)
  48. {
  49. const ZSTD_compressionParameters* const cParams = &ms->cParams;
  50. U32* const hashLarge = ms->hashTable;
  51. U32 const hBitsL = cParams->hashLog;
  52. U32 const mls = cParams->minMatch;
  53. U32* const hashSmall = ms->chainTable;
  54. U32 const hBitsS = cParams->chainLog;
  55. const BYTE* const base = ms->window.base;
  56. const BYTE* ip = base + ms->nextToUpdate;
  57. const BYTE* const iend = ((const BYTE*)end) - HASH_READ_SIZE;
  58. const U32 fastHashFillStep = 3;
  59. /* Always insert every fastHashFillStep position into the hash tables.
  60. * Insert the other positions into the large hash table if their entry
  61. * is empty.
  62. */
  63. for (; ip + fastHashFillStep - 1 <= iend; ip += fastHashFillStep) {
  64. U32 const curr = (U32)(ip - base);
  65. U32 i;
  66. for (i = 0; i < fastHashFillStep; ++i) {
  67. size_t const smHash = ZSTD_hashPtr(ip + i, hBitsS, mls);
  68. size_t const lgHash = ZSTD_hashPtr(ip + i, hBitsL, 8);
  69. if (i == 0)
  70. hashSmall[smHash] = curr + i;
  71. if (i == 0 || hashLarge[lgHash] == 0)
  72. hashLarge[lgHash] = curr + i;
  73. /* Only load extra positions for ZSTD_dtlm_full */
  74. if (dtlm == ZSTD_dtlm_fast)
  75. break;
  76. } }
  77. }
  78. void ZSTD_fillDoubleHashTable(ZSTD_matchState_t* ms,
  79. const void* const end,
  80. ZSTD_dictTableLoadMethod_e dtlm,
  81. ZSTD_tableFillPurpose_e tfp)
  82. {
  83. if (tfp == ZSTD_tfp_forCDict) {
  84. ZSTD_fillDoubleHashTableForCDict(ms, end, dtlm);
  85. } else {
  86. ZSTD_fillDoubleHashTableForCCtx(ms, end, dtlm);
  87. }
  88. }
  89. FORCE_INLINE_TEMPLATE
  90. size_t ZSTD_compressBlock_doubleFast_noDict_generic(
  91. ZSTD_matchState_t* ms, seqStore_t* seqStore, U32 rep[ZSTD_REP_NUM],
  92. void const* src, size_t srcSize, U32 const mls /* template */)
  93. {
  94. ZSTD_compressionParameters const* cParams = &ms->cParams;
  95. U32* const hashLong = ms->hashTable;
  96. const U32 hBitsL = cParams->hashLog;
  97. U32* const hashSmall = ms->chainTable;
  98. const U32 hBitsS = cParams->chainLog;
  99. const BYTE* const base = ms->window.base;
  100. const BYTE* const istart = (const BYTE*)src;
  101. const BYTE* anchor = istart;
  102. const U32 endIndex = (U32)((size_t)(istart - base) + srcSize);
  103. /* presumes that, if there is a dictionary, it must be using Attach mode */
  104. const U32 prefixLowestIndex = ZSTD_getLowestPrefixIndex(ms, endIndex, cParams->windowLog);
  105. const BYTE* const prefixLowest = base + prefixLowestIndex;
  106. const BYTE* const iend = istart + srcSize;
  107. const BYTE* const ilimit = iend - HASH_READ_SIZE;
  108. U32 offset_1=rep[0], offset_2=rep[1];
  109. U32 offsetSaved1 = 0, offsetSaved2 = 0;
  110. size_t mLength;
  111. U32 offset;
  112. U32 curr;
  113. /* how many positions to search before increasing step size */
  114. const size_t kStepIncr = 1 << kSearchStrength;
  115. /* the position at which to increment the step size if no match is found */
  116. const BYTE* nextStep;
  117. size_t step; /* the current step size */
  118. size_t hl0; /* the long hash at ip */
  119. size_t hl1; /* the long hash at ip1 */
  120. U32 idxl0; /* the long match index for ip */
  121. U32 idxl1; /* the long match index for ip1 */
  122. const BYTE* matchl0; /* the long match for ip */
  123. const BYTE* matchs0; /* the short match for ip */
  124. const BYTE* matchl1; /* the long match for ip1 */
  125. const BYTE* ip = istart; /* the current position */
  126. const BYTE* ip1; /* the next position */
  127. DEBUGLOG(5, "ZSTD_compressBlock_doubleFast_noDict_generic");
  128. /* init */
  129. ip += ((ip - prefixLowest) == 0);
  130. {
  131. U32 const current = (U32)(ip - base);
  132. U32 const windowLow = ZSTD_getLowestPrefixIndex(ms, current, cParams->windowLog);
  133. U32 const maxRep = current - windowLow;
  134. if (offset_2 > maxRep) offsetSaved2 = offset_2, offset_2 = 0;
  135. if (offset_1 > maxRep) offsetSaved1 = offset_1, offset_1 = 0;
  136. }
  137. /* Outer Loop: one iteration per match found and stored */
  138. while (1) {
  139. step = 1;
  140. nextStep = ip + kStepIncr;
  141. ip1 = ip + step;
  142. if (ip1 > ilimit) {
  143. goto _cleanup;
  144. }
  145. hl0 = ZSTD_hashPtr(ip, hBitsL, 8);
  146. idxl0 = hashLong[hl0];
  147. matchl0 = base + idxl0;
  148. /* Inner Loop: one iteration per search / position */
  149. do {
  150. const size_t hs0 = ZSTD_hashPtr(ip, hBitsS, mls);
  151. const U32 idxs0 = hashSmall[hs0];
  152. curr = (U32)(ip-base);
  153. matchs0 = base + idxs0;
  154. hashLong[hl0] = hashSmall[hs0] = curr; /* update hash tables */
  155. /* check noDict repcode */
  156. if ((offset_1 > 0) & (MEM_read32(ip+1-offset_1) == MEM_read32(ip+1))) {
  157. mLength = ZSTD_count(ip+1+4, ip+1+4-offset_1, iend) + 4;
  158. ip++;
  159. ZSTD_storeSeq(seqStore, (size_t)(ip-anchor), anchor, iend, REPCODE1_TO_OFFBASE, mLength);
  160. goto _match_stored;
  161. }
  162. hl1 = ZSTD_hashPtr(ip1, hBitsL, 8);
  163. if (idxl0 > prefixLowestIndex) {
  164. /* check prefix long match */
  165. if (MEM_read64(matchl0) == MEM_read64(ip)) {
  166. mLength = ZSTD_count(ip+8, matchl0+8, iend) + 8;
  167. offset = (U32)(ip-matchl0);
  168. while (((ip>anchor) & (matchl0>prefixLowest)) && (ip[-1] == matchl0[-1])) { ip--; matchl0--; mLength++; } /* catch up */
  169. goto _match_found;
  170. }
  171. }
  172. idxl1 = hashLong[hl1];
  173. matchl1 = base + idxl1;
  174. if (idxs0 > prefixLowestIndex) {
  175. /* check prefix short match */
  176. if (MEM_read32(matchs0) == MEM_read32(ip)) {
  177. goto _search_next_long;
  178. }
  179. }
  180. if (ip1 >= nextStep) {
  181. PREFETCH_L1(ip1 + 64);
  182. PREFETCH_L1(ip1 + 128);
  183. step++;
  184. nextStep += kStepIncr;
  185. }
  186. ip = ip1;
  187. ip1 += step;
  188. hl0 = hl1;
  189. idxl0 = idxl1;
  190. matchl0 = matchl1;
  191. #if defined(__aarch64__)
  192. PREFETCH_L1(ip+256);
  193. #endif
  194. } while (ip1 <= ilimit);
  195. _cleanup:
  196. /* If offset_1 started invalid (offsetSaved1 != 0) and became valid (offset_1 != 0),
  197. * rotate saved offsets. See comment in ZSTD_compressBlock_fast_noDict for more context. */
  198. offsetSaved2 = ((offsetSaved1 != 0) && (offset_1 != 0)) ? offsetSaved1 : offsetSaved2;
  199. /* save reps for next block */
  200. rep[0] = offset_1 ? offset_1 : offsetSaved1;
  201. rep[1] = offset_2 ? offset_2 : offsetSaved2;
  202. /* Return the last literals size */
  203. return (size_t)(iend - anchor);
  204. _search_next_long:
  205. /* check prefix long +1 match */
  206. if (idxl1 > prefixLowestIndex) {
  207. if (MEM_read64(matchl1) == MEM_read64(ip1)) {
  208. ip = ip1;
  209. mLength = ZSTD_count(ip+8, matchl1+8, iend) + 8;
  210. offset = (U32)(ip-matchl1);
  211. while (((ip>anchor) & (matchl1>prefixLowest)) && (ip[-1] == matchl1[-1])) { ip--; matchl1--; mLength++; } /* catch up */
  212. goto _match_found;
  213. }
  214. }
  215. /* if no long +1 match, explore the short match we found */
  216. mLength = ZSTD_count(ip+4, matchs0+4, iend) + 4;
  217. offset = (U32)(ip - matchs0);
  218. while (((ip>anchor) & (matchs0>prefixLowest)) && (ip[-1] == matchs0[-1])) { ip--; matchs0--; mLength++; } /* catch up */
  219. /* fall-through */
  220. _match_found: /* requires ip, offset, mLength */
  221. offset_2 = offset_1;
  222. offset_1 = offset;
  223. if (step < 4) {
  224. /* It is unsafe to write this value back to the hashtable when ip1 is
  225. * greater than or equal to the new ip we will have after we're done
  226. * processing this match. Rather than perform that test directly
  227. * (ip1 >= ip + mLength), which costs speed in practice, we do a simpler
  228. * more predictable test. The minmatch even if we take a short match is
  229. * 4 bytes, so as long as step, the distance between ip and ip1
  230. * (initially) is less than 4, we know ip1 < new ip. */
  231. hashLong[hl1] = (U32)(ip1 - base);
  232. }
  233. ZSTD_storeSeq(seqStore, (size_t)(ip-anchor), anchor, iend, OFFSET_TO_OFFBASE(offset), mLength);
  234. _match_stored:
  235. /* match found */
  236. ip += mLength;
  237. anchor = ip;
  238. if (ip <= ilimit) {
  239. /* Complementary insertion */
  240. /* done after iLimit test, as candidates could be > iend-8 */
  241. { U32 const indexToInsert = curr+2;
  242. hashLong[ZSTD_hashPtr(base+indexToInsert, hBitsL, 8)] = indexToInsert;
  243. hashLong[ZSTD_hashPtr(ip-2, hBitsL, 8)] = (U32)(ip-2-base);
  244. hashSmall[ZSTD_hashPtr(base+indexToInsert, hBitsS, mls)] = indexToInsert;
  245. hashSmall[ZSTD_hashPtr(ip-1, hBitsS, mls)] = (U32)(ip-1-base);
  246. }
  247. /* check immediate repcode */
  248. while ( (ip <= ilimit)
  249. && ( (offset_2>0)
  250. & (MEM_read32(ip) == MEM_read32(ip - offset_2)) )) {
  251. /* store sequence */
  252. size_t const rLength = ZSTD_count(ip+4, ip+4-offset_2, iend) + 4;
  253. U32 const tmpOff = offset_2; offset_2 = offset_1; offset_1 = tmpOff; /* swap offset_2 <=> offset_1 */
  254. hashSmall[ZSTD_hashPtr(ip, hBitsS, mls)] = (U32)(ip-base);
  255. hashLong[ZSTD_hashPtr(ip, hBitsL, 8)] = (U32)(ip-base);
  256. ZSTD_storeSeq(seqStore, 0, anchor, iend, REPCODE1_TO_OFFBASE, rLength);
  257. ip += rLength;
  258. anchor = ip;
  259. continue; /* faster when present ... (?) */
  260. }
  261. }
  262. }
  263. }
  264. FORCE_INLINE_TEMPLATE
  265. size_t ZSTD_compressBlock_doubleFast_dictMatchState_generic(
  266. ZSTD_matchState_t* ms, seqStore_t* seqStore, U32 rep[ZSTD_REP_NUM],
  267. void const* src, size_t srcSize,
  268. U32 const mls /* template */)
  269. {
  270. ZSTD_compressionParameters const* cParams = &ms->cParams;
  271. U32* const hashLong = ms->hashTable;
  272. const U32 hBitsL = cParams->hashLog;
  273. U32* const hashSmall = ms->chainTable;
  274. const U32 hBitsS = cParams->chainLog;
  275. const BYTE* const base = ms->window.base;
  276. const BYTE* const istart = (const BYTE*)src;
  277. const BYTE* ip = istart;
  278. const BYTE* anchor = istart;
  279. const U32 endIndex = (U32)((size_t)(istart - base) + srcSize);
  280. /* presumes that, if there is a dictionary, it must be using Attach mode */
  281. const U32 prefixLowestIndex = ZSTD_getLowestPrefixIndex(ms, endIndex, cParams->windowLog);
  282. const BYTE* const prefixLowest = base + prefixLowestIndex;
  283. const BYTE* const iend = istart + srcSize;
  284. const BYTE* const ilimit = iend - HASH_READ_SIZE;
  285. U32 offset_1=rep[0], offset_2=rep[1];
  286. const ZSTD_matchState_t* const dms = ms->dictMatchState;
  287. const ZSTD_compressionParameters* const dictCParams = &dms->cParams;
  288. const U32* const dictHashLong = dms->hashTable;
  289. const U32* const dictHashSmall = dms->chainTable;
  290. const U32 dictStartIndex = dms->window.dictLimit;
  291. const BYTE* const dictBase = dms->window.base;
  292. const BYTE* const dictStart = dictBase + dictStartIndex;
  293. const BYTE* const dictEnd = dms->window.nextSrc;
  294. const U32 dictIndexDelta = prefixLowestIndex - (U32)(dictEnd - dictBase);
  295. const U32 dictHBitsL = dictCParams->hashLog + ZSTD_SHORT_CACHE_TAG_BITS;
  296. const U32 dictHBitsS = dictCParams->chainLog + ZSTD_SHORT_CACHE_TAG_BITS;
  297. const U32 dictAndPrefixLength = (U32)((ip - prefixLowest) + (dictEnd - dictStart));
  298. DEBUGLOG(5, "ZSTD_compressBlock_doubleFast_dictMatchState_generic");
  299. /* if a dictionary is attached, it must be within window range */
  300. assert(ms->window.dictLimit + (1U << cParams->windowLog) >= endIndex);
  301. if (ms->prefetchCDictTables) {
  302. size_t const hashTableBytes = (((size_t)1) << dictCParams->hashLog) * sizeof(U32);
  303. size_t const chainTableBytes = (((size_t)1) << dictCParams->chainLog) * sizeof(U32);
  304. PREFETCH_AREA(dictHashLong, hashTableBytes)
  305. PREFETCH_AREA(dictHashSmall, chainTableBytes)
  306. }
  307. /* init */
  308. ip += (dictAndPrefixLength == 0);
  309. /* dictMatchState repCode checks don't currently handle repCode == 0
  310. * disabling. */
  311. assert(offset_1 <= dictAndPrefixLength);
  312. assert(offset_2 <= dictAndPrefixLength);
  313. /* Main Search Loop */
  314. while (ip < ilimit) { /* < instead of <=, because repcode check at (ip+1) */
  315. size_t mLength;
  316. U32 offset;
  317. size_t const h2 = ZSTD_hashPtr(ip, hBitsL, 8);
  318. size_t const h = ZSTD_hashPtr(ip, hBitsS, mls);
  319. size_t const dictHashAndTagL = ZSTD_hashPtr(ip, dictHBitsL, 8);
  320. size_t const dictHashAndTagS = ZSTD_hashPtr(ip, dictHBitsS, mls);
  321. U32 const dictMatchIndexAndTagL = dictHashLong[dictHashAndTagL >> ZSTD_SHORT_CACHE_TAG_BITS];
  322. U32 const dictMatchIndexAndTagS = dictHashSmall[dictHashAndTagS >> ZSTD_SHORT_CACHE_TAG_BITS];
  323. int const dictTagsMatchL = ZSTD_comparePackedTags(dictMatchIndexAndTagL, dictHashAndTagL);
  324. int const dictTagsMatchS = ZSTD_comparePackedTags(dictMatchIndexAndTagS, dictHashAndTagS);
  325. U32 const curr = (U32)(ip-base);
  326. U32 const matchIndexL = hashLong[h2];
  327. U32 matchIndexS = hashSmall[h];
  328. const BYTE* matchLong = base + matchIndexL;
  329. const BYTE* match = base + matchIndexS;
  330. const U32 repIndex = curr + 1 - offset_1;
  331. const BYTE* repMatch = (repIndex < prefixLowestIndex) ?
  332. dictBase + (repIndex - dictIndexDelta) :
  333. base + repIndex;
  334. hashLong[h2] = hashSmall[h] = curr; /* update hash tables */
  335. /* check repcode */
  336. if (((U32)((prefixLowestIndex-1) - repIndex) >= 3 /* intentional underflow */)
  337. && (MEM_read32(repMatch) == MEM_read32(ip+1)) ) {
  338. const BYTE* repMatchEnd = repIndex < prefixLowestIndex ? dictEnd : iend;
  339. mLength = ZSTD_count_2segments(ip+1+4, repMatch+4, iend, repMatchEnd, prefixLowest) + 4;
  340. ip++;
  341. ZSTD_storeSeq(seqStore, (size_t)(ip-anchor), anchor, iend, REPCODE1_TO_OFFBASE, mLength);
  342. goto _match_stored;
  343. }
  344. if (matchIndexL > prefixLowestIndex) {
  345. /* check prefix long match */
  346. if (MEM_read64(matchLong) == MEM_read64(ip)) {
  347. mLength = ZSTD_count(ip+8, matchLong+8, iend) + 8;
  348. offset = (U32)(ip-matchLong);
  349. while (((ip>anchor) & (matchLong>prefixLowest)) && (ip[-1] == matchLong[-1])) { ip--; matchLong--; mLength++; } /* catch up */
  350. goto _match_found;
  351. }
  352. } else if (dictTagsMatchL) {
  353. /* check dictMatchState long match */
  354. U32 const dictMatchIndexL = dictMatchIndexAndTagL >> ZSTD_SHORT_CACHE_TAG_BITS;
  355. const BYTE* dictMatchL = dictBase + dictMatchIndexL;
  356. assert(dictMatchL < dictEnd);
  357. if (dictMatchL > dictStart && MEM_read64(dictMatchL) == MEM_read64(ip)) {
  358. mLength = ZSTD_count_2segments(ip+8, dictMatchL+8, iend, dictEnd, prefixLowest) + 8;
  359. offset = (U32)(curr - dictMatchIndexL - dictIndexDelta);
  360. while (((ip>anchor) & (dictMatchL>dictStart)) && (ip[-1] == dictMatchL[-1])) { ip--; dictMatchL--; mLength++; } /* catch up */
  361. goto _match_found;
  362. } }
  363. if (matchIndexS > prefixLowestIndex) {
  364. /* check prefix short match */
  365. if (MEM_read32(match) == MEM_read32(ip)) {
  366. goto _search_next_long;
  367. }
  368. } else if (dictTagsMatchS) {
  369. /* check dictMatchState short match */
  370. U32 const dictMatchIndexS = dictMatchIndexAndTagS >> ZSTD_SHORT_CACHE_TAG_BITS;
  371. match = dictBase + dictMatchIndexS;
  372. matchIndexS = dictMatchIndexS + dictIndexDelta;
  373. if (match > dictStart && MEM_read32(match) == MEM_read32(ip)) {
  374. goto _search_next_long;
  375. } }
  376. ip += ((ip-anchor) >> kSearchStrength) + 1;
  377. #if defined(__aarch64__)
  378. PREFETCH_L1(ip+256);
  379. #endif
  380. continue;
  381. _search_next_long:
  382. { size_t const hl3 = ZSTD_hashPtr(ip+1, hBitsL, 8);
  383. size_t const dictHashAndTagL3 = ZSTD_hashPtr(ip+1, dictHBitsL, 8);
  384. U32 const matchIndexL3 = hashLong[hl3];
  385. U32 const dictMatchIndexAndTagL3 = dictHashLong[dictHashAndTagL3 >> ZSTD_SHORT_CACHE_TAG_BITS];
  386. int const dictTagsMatchL3 = ZSTD_comparePackedTags(dictMatchIndexAndTagL3, dictHashAndTagL3);
  387. const BYTE* matchL3 = base + matchIndexL3;
  388. hashLong[hl3] = curr + 1;
  389. /* check prefix long +1 match */
  390. if (matchIndexL3 > prefixLowestIndex) {
  391. if (MEM_read64(matchL3) == MEM_read64(ip+1)) {
  392. mLength = ZSTD_count(ip+9, matchL3+8, iend) + 8;
  393. ip++;
  394. offset = (U32)(ip-matchL3);
  395. while (((ip>anchor) & (matchL3>prefixLowest)) && (ip[-1] == matchL3[-1])) { ip--; matchL3--; mLength++; } /* catch up */
  396. goto _match_found;
  397. }
  398. } else if (dictTagsMatchL3) {
  399. /* check dict long +1 match */
  400. U32 const dictMatchIndexL3 = dictMatchIndexAndTagL3 >> ZSTD_SHORT_CACHE_TAG_BITS;
  401. const BYTE* dictMatchL3 = dictBase + dictMatchIndexL3;
  402. assert(dictMatchL3 < dictEnd);
  403. if (dictMatchL3 > dictStart && MEM_read64(dictMatchL3) == MEM_read64(ip+1)) {
  404. mLength = ZSTD_count_2segments(ip+1+8, dictMatchL3+8, iend, dictEnd, prefixLowest) + 8;
  405. ip++;
  406. offset = (U32)(curr + 1 - dictMatchIndexL3 - dictIndexDelta);
  407. while (((ip>anchor) & (dictMatchL3>dictStart)) && (ip[-1] == dictMatchL3[-1])) { ip--; dictMatchL3--; mLength++; } /* catch up */
  408. goto _match_found;
  409. } } }
  410. /* if no long +1 match, explore the short match we found */
  411. if (matchIndexS < prefixLowestIndex) {
  412. mLength = ZSTD_count_2segments(ip+4, match+4, iend, dictEnd, prefixLowest) + 4;
  413. offset = (U32)(curr - matchIndexS);
  414. while (((ip>anchor) & (match>dictStart)) && (ip[-1] == match[-1])) { ip--; match--; mLength++; } /* catch up */
  415. } else {
  416. mLength = ZSTD_count(ip+4, match+4, iend) + 4;
  417. offset = (U32)(ip - match);
  418. while (((ip>anchor) & (match>prefixLowest)) && (ip[-1] == match[-1])) { ip--; match--; mLength++; } /* catch up */
  419. }
  420. _match_found:
  421. offset_2 = offset_1;
  422. offset_1 = offset;
  423. ZSTD_storeSeq(seqStore, (size_t)(ip-anchor), anchor, iend, OFFSET_TO_OFFBASE(offset), mLength);
  424. _match_stored:
  425. /* match found */
  426. ip += mLength;
  427. anchor = ip;
  428. if (ip <= ilimit) {
  429. /* Complementary insertion */
  430. /* done after iLimit test, as candidates could be > iend-8 */
  431. { U32 const indexToInsert = curr+2;
  432. hashLong[ZSTD_hashPtr(base+indexToInsert, hBitsL, 8)] = indexToInsert;
  433. hashLong[ZSTD_hashPtr(ip-2, hBitsL, 8)] = (U32)(ip-2-base);
  434. hashSmall[ZSTD_hashPtr(base+indexToInsert, hBitsS, mls)] = indexToInsert;
  435. hashSmall[ZSTD_hashPtr(ip-1, hBitsS, mls)] = (U32)(ip-1-base);
  436. }
  437. /* check immediate repcode */
  438. while (ip <= ilimit) {
  439. U32 const current2 = (U32)(ip-base);
  440. U32 const repIndex2 = current2 - offset_2;
  441. const BYTE* repMatch2 = repIndex2 < prefixLowestIndex ?
  442. dictBase + repIndex2 - dictIndexDelta :
  443. base + repIndex2;
  444. if ( ((U32)((prefixLowestIndex-1) - (U32)repIndex2) >= 3 /* intentional overflow */)
  445. && (MEM_read32(repMatch2) == MEM_read32(ip)) ) {
  446. const BYTE* const repEnd2 = repIndex2 < prefixLowestIndex ? dictEnd : iend;
  447. size_t const repLength2 = ZSTD_count_2segments(ip+4, repMatch2+4, iend, repEnd2, prefixLowest) + 4;
  448. U32 tmpOffset = offset_2; offset_2 = offset_1; offset_1 = tmpOffset; /* swap offset_2 <=> offset_1 */
  449. ZSTD_storeSeq(seqStore, 0, anchor, iend, REPCODE1_TO_OFFBASE, repLength2);
  450. hashSmall[ZSTD_hashPtr(ip, hBitsS, mls)] = current2;
  451. hashLong[ZSTD_hashPtr(ip, hBitsL, 8)] = current2;
  452. ip += repLength2;
  453. anchor = ip;
  454. continue;
  455. }
  456. break;
  457. }
  458. }
  459. } /* while (ip < ilimit) */
  460. /* save reps for next block */
  461. rep[0] = offset_1;
  462. rep[1] = offset_2;
  463. /* Return the last literals size */
  464. return (size_t)(iend - anchor);
  465. }
  466. #define ZSTD_GEN_DFAST_FN(dictMode, mls) \
  467. static size_t ZSTD_compressBlock_doubleFast_##dictMode##_##mls( \
  468. ZSTD_matchState_t* ms, seqStore_t* seqStore, U32 rep[ZSTD_REP_NUM], \
  469. void const* src, size_t srcSize) \
  470. { \
  471. return ZSTD_compressBlock_doubleFast_##dictMode##_generic(ms, seqStore, rep, src, srcSize, mls); \
  472. }
  473. ZSTD_GEN_DFAST_FN(noDict, 4)
  474. ZSTD_GEN_DFAST_FN(noDict, 5)
  475. ZSTD_GEN_DFAST_FN(noDict, 6)
  476. ZSTD_GEN_DFAST_FN(noDict, 7)
  477. ZSTD_GEN_DFAST_FN(dictMatchState, 4)
  478. ZSTD_GEN_DFAST_FN(dictMatchState, 5)
  479. ZSTD_GEN_DFAST_FN(dictMatchState, 6)
  480. ZSTD_GEN_DFAST_FN(dictMatchState, 7)
  481. size_t ZSTD_compressBlock_doubleFast(
  482. ZSTD_matchState_t* ms, seqStore_t* seqStore, U32 rep[ZSTD_REP_NUM],
  483. void const* src, size_t srcSize)
  484. {
  485. const U32 mls = ms->cParams.minMatch;
  486. switch(mls)
  487. {
  488. default: /* includes case 3 */
  489. case 4 :
  490. return ZSTD_compressBlock_doubleFast_noDict_4(ms, seqStore, rep, src, srcSize);
  491. case 5 :
  492. return ZSTD_compressBlock_doubleFast_noDict_5(ms, seqStore, rep, src, srcSize);
  493. case 6 :
  494. return ZSTD_compressBlock_doubleFast_noDict_6(ms, seqStore, rep, src, srcSize);
  495. case 7 :
  496. return ZSTD_compressBlock_doubleFast_noDict_7(ms, seqStore, rep, src, srcSize);
  497. }
  498. }
  499. size_t ZSTD_compressBlock_doubleFast_dictMatchState(
  500. ZSTD_matchState_t* ms, seqStore_t* seqStore, U32 rep[ZSTD_REP_NUM],
  501. void const* src, size_t srcSize)
  502. {
  503. const U32 mls = ms->cParams.minMatch;
  504. switch(mls)
  505. {
  506. default: /* includes case 3 */
  507. case 4 :
  508. return ZSTD_compressBlock_doubleFast_dictMatchState_4(ms, seqStore, rep, src, srcSize);
  509. case 5 :
  510. return ZSTD_compressBlock_doubleFast_dictMatchState_5(ms, seqStore, rep, src, srcSize);
  511. case 6 :
  512. return ZSTD_compressBlock_doubleFast_dictMatchState_6(ms, seqStore, rep, src, srcSize);
  513. case 7 :
  514. return ZSTD_compressBlock_doubleFast_dictMatchState_7(ms, seqStore, rep, src, srcSize);
  515. }
  516. }
  517. static size_t ZSTD_compressBlock_doubleFast_extDict_generic(
  518. ZSTD_matchState_t* ms, seqStore_t* seqStore, U32 rep[ZSTD_REP_NUM],
  519. void const* src, size_t srcSize,
  520. U32 const mls /* template */)
  521. {
  522. ZSTD_compressionParameters const* cParams = &ms->cParams;
  523. U32* const hashLong = ms->hashTable;
  524. U32 const hBitsL = cParams->hashLog;
  525. U32* const hashSmall = ms->chainTable;
  526. U32 const hBitsS = cParams->chainLog;
  527. const BYTE* const istart = (const BYTE*)src;
  528. const BYTE* ip = istart;
  529. const BYTE* anchor = istart;
  530. const BYTE* const iend = istart + srcSize;
  531. const BYTE* const ilimit = iend - 8;
  532. const BYTE* const base = ms->window.base;
  533. const U32 endIndex = (U32)((size_t)(istart - base) + srcSize);
  534. const U32 lowLimit = ZSTD_getLowestMatchIndex(ms, endIndex, cParams->windowLog);
  535. const U32 dictStartIndex = lowLimit;
  536. const U32 dictLimit = ms->window.dictLimit;
  537. const U32 prefixStartIndex = (dictLimit > lowLimit) ? dictLimit : lowLimit;
  538. const BYTE* const prefixStart = base + prefixStartIndex;
  539. const BYTE* const dictBase = ms->window.dictBase;
  540. const BYTE* const dictStart = dictBase + dictStartIndex;
  541. const BYTE* const dictEnd = dictBase + prefixStartIndex;
  542. U32 offset_1=rep[0], offset_2=rep[1];
  543. DEBUGLOG(5, "ZSTD_compressBlock_doubleFast_extDict_generic (srcSize=%zu)", srcSize);
  544. /* if extDict is invalidated due to maxDistance, switch to "regular" variant */
  545. if (prefixStartIndex == dictStartIndex)
  546. return ZSTD_compressBlock_doubleFast(ms, seqStore, rep, src, srcSize);
  547. /* Search Loop */
  548. while (ip < ilimit) { /* < instead of <=, because (ip+1) */
  549. const size_t hSmall = ZSTD_hashPtr(ip, hBitsS, mls);
  550. const U32 matchIndex = hashSmall[hSmall];
  551. const BYTE* const matchBase = matchIndex < prefixStartIndex ? dictBase : base;
  552. const BYTE* match = matchBase + matchIndex;
  553. const size_t hLong = ZSTD_hashPtr(ip, hBitsL, 8);
  554. const U32 matchLongIndex = hashLong[hLong];
  555. const BYTE* const matchLongBase = matchLongIndex < prefixStartIndex ? dictBase : base;
  556. const BYTE* matchLong = matchLongBase + matchLongIndex;
  557. const U32 curr = (U32)(ip-base);
  558. const U32 repIndex = curr + 1 - offset_1; /* offset_1 expected <= curr +1 */
  559. const BYTE* const repBase = repIndex < prefixStartIndex ? dictBase : base;
  560. const BYTE* const repMatch = repBase + repIndex;
  561. size_t mLength;
  562. hashSmall[hSmall] = hashLong[hLong] = curr; /* update hash table */
  563. if ((((U32)((prefixStartIndex-1) - repIndex) >= 3) /* intentional underflow : ensure repIndex doesn't overlap dict + prefix */
  564. & (offset_1 <= curr+1 - dictStartIndex)) /* note: we are searching at curr+1 */
  565. && (MEM_read32(repMatch) == MEM_read32(ip+1)) ) {
  566. const BYTE* repMatchEnd = repIndex < prefixStartIndex ? dictEnd : iend;
  567. mLength = ZSTD_count_2segments(ip+1+4, repMatch+4, iend, repMatchEnd, prefixStart) + 4;
  568. ip++;
  569. ZSTD_storeSeq(seqStore, (size_t)(ip-anchor), anchor, iend, REPCODE1_TO_OFFBASE, mLength);
  570. } else {
  571. if ((matchLongIndex > dictStartIndex) && (MEM_read64(matchLong) == MEM_read64(ip))) {
  572. const BYTE* const matchEnd = matchLongIndex < prefixStartIndex ? dictEnd : iend;
  573. const BYTE* const lowMatchPtr = matchLongIndex < prefixStartIndex ? dictStart : prefixStart;
  574. U32 offset;
  575. mLength = ZSTD_count_2segments(ip+8, matchLong+8, iend, matchEnd, prefixStart) + 8;
  576. offset = curr - matchLongIndex;
  577. while (((ip>anchor) & (matchLong>lowMatchPtr)) && (ip[-1] == matchLong[-1])) { ip--; matchLong--; mLength++; } /* catch up */
  578. offset_2 = offset_1;
  579. offset_1 = offset;
  580. ZSTD_storeSeq(seqStore, (size_t)(ip-anchor), anchor, iend, OFFSET_TO_OFFBASE(offset), mLength);
  581. } else if ((matchIndex > dictStartIndex) && (MEM_read32(match) == MEM_read32(ip))) {
  582. size_t const h3 = ZSTD_hashPtr(ip+1, hBitsL, 8);
  583. U32 const matchIndex3 = hashLong[h3];
  584. const BYTE* const match3Base = matchIndex3 < prefixStartIndex ? dictBase : base;
  585. const BYTE* match3 = match3Base + matchIndex3;
  586. U32 offset;
  587. hashLong[h3] = curr + 1;
  588. if ( (matchIndex3 > dictStartIndex) && (MEM_read64(match3) == MEM_read64(ip+1)) ) {
  589. const BYTE* const matchEnd = matchIndex3 < prefixStartIndex ? dictEnd : iend;
  590. const BYTE* const lowMatchPtr = matchIndex3 < prefixStartIndex ? dictStart : prefixStart;
  591. mLength = ZSTD_count_2segments(ip+9, match3+8, iend, matchEnd, prefixStart) + 8;
  592. ip++;
  593. offset = curr+1 - matchIndex3;
  594. while (((ip>anchor) & (match3>lowMatchPtr)) && (ip[-1] == match3[-1])) { ip--; match3--; mLength++; } /* catch up */
  595. } else {
  596. const BYTE* const matchEnd = matchIndex < prefixStartIndex ? dictEnd : iend;
  597. const BYTE* const lowMatchPtr = matchIndex < prefixStartIndex ? dictStart : prefixStart;
  598. mLength = ZSTD_count_2segments(ip+4, match+4, iend, matchEnd, prefixStart) + 4;
  599. offset = curr - matchIndex;
  600. while (((ip>anchor) & (match>lowMatchPtr)) && (ip[-1] == match[-1])) { ip--; match--; mLength++; } /* catch up */
  601. }
  602. offset_2 = offset_1;
  603. offset_1 = offset;
  604. ZSTD_storeSeq(seqStore, (size_t)(ip-anchor), anchor, iend, OFFSET_TO_OFFBASE(offset), mLength);
  605. } else {
  606. ip += ((ip-anchor) >> kSearchStrength) + 1;
  607. continue;
  608. } }
  609. /* move to next sequence start */
  610. ip += mLength;
  611. anchor = ip;
  612. if (ip <= ilimit) {
  613. /* Complementary insertion */
  614. /* done after iLimit test, as candidates could be > iend-8 */
  615. { U32 const indexToInsert = curr+2;
  616. hashLong[ZSTD_hashPtr(base+indexToInsert, hBitsL, 8)] = indexToInsert;
  617. hashLong[ZSTD_hashPtr(ip-2, hBitsL, 8)] = (U32)(ip-2-base);
  618. hashSmall[ZSTD_hashPtr(base+indexToInsert, hBitsS, mls)] = indexToInsert;
  619. hashSmall[ZSTD_hashPtr(ip-1, hBitsS, mls)] = (U32)(ip-1-base);
  620. }
  621. /* check immediate repcode */
  622. while (ip <= ilimit) {
  623. U32 const current2 = (U32)(ip-base);
  624. U32 const repIndex2 = current2 - offset_2;
  625. const BYTE* repMatch2 = repIndex2 < prefixStartIndex ? dictBase + repIndex2 : base + repIndex2;
  626. if ( (((U32)((prefixStartIndex-1) - repIndex2) >= 3) /* intentional overflow : ensure repIndex2 doesn't overlap dict + prefix */
  627. & (offset_2 <= current2 - dictStartIndex))
  628. && (MEM_read32(repMatch2) == MEM_read32(ip)) ) {
  629. const BYTE* const repEnd2 = repIndex2 < prefixStartIndex ? dictEnd : iend;
  630. size_t const repLength2 = ZSTD_count_2segments(ip+4, repMatch2+4, iend, repEnd2, prefixStart) + 4;
  631. U32 const tmpOffset = offset_2; offset_2 = offset_1; offset_1 = tmpOffset; /* swap offset_2 <=> offset_1 */
  632. ZSTD_storeSeq(seqStore, 0, anchor, iend, REPCODE1_TO_OFFBASE, repLength2);
  633. hashSmall[ZSTD_hashPtr(ip, hBitsS, mls)] = current2;
  634. hashLong[ZSTD_hashPtr(ip, hBitsL, 8)] = current2;
  635. ip += repLength2;
  636. anchor = ip;
  637. continue;
  638. }
  639. break;
  640. } } }
  641. /* save reps for next block */
  642. rep[0] = offset_1;
  643. rep[1] = offset_2;
  644. /* Return the last literals size */
  645. return (size_t)(iend - anchor);
  646. }
  647. ZSTD_GEN_DFAST_FN(extDict, 4)
  648. ZSTD_GEN_DFAST_FN(extDict, 5)
  649. ZSTD_GEN_DFAST_FN(extDict, 6)
  650. ZSTD_GEN_DFAST_FN(extDict, 7)
  651. size_t ZSTD_compressBlock_doubleFast_extDict(
  652. ZSTD_matchState_t* ms, seqStore_t* seqStore, U32 rep[ZSTD_REP_NUM],
  653. void const* src, size_t srcSize)
  654. {
  655. U32 const mls = ms->cParams.minMatch;
  656. switch(mls)
  657. {
  658. default: /* includes case 3 */
  659. case 4 :
  660. return ZSTD_compressBlock_doubleFast_extDict_4(ms, seqStore, rep, src, srcSize);
  661. case 5 :
  662. return ZSTD_compressBlock_doubleFast_extDict_5(ms, seqStore, rep, src, srcSize);
  663. case 6 :
  664. return ZSTD_compressBlock_doubleFast_extDict_6(ms, seqStore, rep, src, srcSize);
  665. case 7 :
  666. return ZSTD_compressBlock_doubleFast_extDict_7(ms, seqStore, rep, src, srcSize);
  667. }
  668. }