zstd_fast.c 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985
  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" /* ZSTD_hashPtr, ZSTD_count, ZSTD_storeSeq */
  11. #include "zstd_fast.h"
  12. static
  13. ZSTD_ALLOW_POINTER_OVERFLOW_ATTR
  14. void ZSTD_fillHashTableForCDict(ZSTD_MatchState_t* ms,
  15. const void* const end,
  16. ZSTD_dictTableLoadMethod_e dtlm)
  17. {
  18. const ZSTD_compressionParameters* const cParams = &ms->cParams;
  19. U32* const hashTable = ms->hashTable;
  20. U32 const hBits = cParams->hashLog + ZSTD_SHORT_CACHE_TAG_BITS;
  21. U32 const mls = cParams->minMatch;
  22. const BYTE* const base = ms->window.base;
  23. const BYTE* ip = base + ms->nextToUpdate;
  24. const BYTE* const iend = ((const BYTE*)end) - HASH_READ_SIZE;
  25. const U32 fastHashFillStep = 3;
  26. /* Currently, we always use ZSTD_dtlm_full for filling CDict tables.
  27. * Feel free to remove this assert if there's a good reason! */
  28. assert(dtlm == ZSTD_dtlm_full);
  29. /* Always insert every fastHashFillStep position into the hash table.
  30. * Insert the other positions if their hash entry is empty.
  31. */
  32. for ( ; ip + fastHashFillStep < iend + 2; ip += fastHashFillStep) {
  33. U32 const curr = (U32)(ip - base);
  34. { size_t const hashAndTag = ZSTD_hashPtr(ip, hBits, mls);
  35. ZSTD_writeTaggedIndex(hashTable, hashAndTag, curr); }
  36. if (dtlm == ZSTD_dtlm_fast) continue;
  37. /* Only load extra positions for ZSTD_dtlm_full */
  38. { U32 p;
  39. for (p = 1; p < fastHashFillStep; ++p) {
  40. size_t const hashAndTag = ZSTD_hashPtr(ip + p, hBits, mls);
  41. if (hashTable[hashAndTag >> ZSTD_SHORT_CACHE_TAG_BITS] == 0) { /* not yet filled */
  42. ZSTD_writeTaggedIndex(hashTable, hashAndTag, curr + p);
  43. } } } }
  44. }
  45. static
  46. ZSTD_ALLOW_POINTER_OVERFLOW_ATTR
  47. void ZSTD_fillHashTableForCCtx(ZSTD_MatchState_t* ms,
  48. const void* const end,
  49. ZSTD_dictTableLoadMethod_e dtlm)
  50. {
  51. const ZSTD_compressionParameters* const cParams = &ms->cParams;
  52. U32* const hashTable = ms->hashTable;
  53. U32 const hBits = cParams->hashLog;
  54. U32 const mls = cParams->minMatch;
  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. /* Currently, we always use ZSTD_dtlm_fast for filling CCtx tables.
  60. * Feel free to remove this assert if there's a good reason! */
  61. assert(dtlm == ZSTD_dtlm_fast);
  62. /* Always insert every fastHashFillStep position into the hash table.
  63. * Insert the other positions if their hash entry is empty.
  64. */
  65. for ( ; ip + fastHashFillStep < iend + 2; ip += fastHashFillStep) {
  66. U32 const curr = (U32)(ip - base);
  67. size_t const hash0 = ZSTD_hashPtr(ip, hBits, mls);
  68. hashTable[hash0] = curr;
  69. if (dtlm == ZSTD_dtlm_fast) continue;
  70. /* Only load extra positions for ZSTD_dtlm_full */
  71. { U32 p;
  72. for (p = 1; p < fastHashFillStep; ++p) {
  73. size_t const hash = ZSTD_hashPtr(ip + p, hBits, mls);
  74. if (hashTable[hash] == 0) { /* not yet filled */
  75. hashTable[hash] = curr + p;
  76. } } } }
  77. }
  78. void ZSTD_fillHashTable(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_fillHashTableForCDict(ms, end, dtlm);
  85. } else {
  86. ZSTD_fillHashTableForCCtx(ms, end, dtlm);
  87. }
  88. }
  89. typedef int (*ZSTD_match4Found) (const BYTE* currentPtr, const BYTE* matchAddress, U32 matchIdx, U32 idxLowLimit);
  90. static int
  91. ZSTD_match4Found_cmov(const BYTE* currentPtr, const BYTE* matchAddress, U32 matchIdx, U32 idxLowLimit)
  92. {
  93. /* Array of ~random data, should have low probability of matching data.
  94. * Load from here if the index is invalid.
  95. * Used to avoid unpredictable branches. */
  96. static const BYTE dummy[] = {0x12,0x34,0x56,0x78};
  97. /* currentIdx >= lowLimit is a (somewhat) unpredictable branch.
  98. * However expression below compiles into conditional move.
  99. */
  100. const BYTE* mvalAddr = ZSTD_selectAddr(matchIdx, idxLowLimit, matchAddress, dummy);
  101. /* Note: this used to be written as : return test1 && test2;
  102. * Unfortunately, once inlined, these tests become branches,
  103. * in which case it becomes critical that they are executed in the right order (test1 then test2).
  104. * So we have to write these tests in a specific manner to ensure their ordering.
  105. */
  106. if (MEM_read32(currentPtr) != MEM_read32(mvalAddr)) return 0;
  107. /* force ordering of these tests, which matters once the function is inlined, as they become branches */
  108. #if defined(__GNUC__)
  109. __asm__("");
  110. #endif
  111. return matchIdx >= idxLowLimit;
  112. }
  113. static int
  114. ZSTD_match4Found_branch(const BYTE* currentPtr, const BYTE* matchAddress, U32 matchIdx, U32 idxLowLimit)
  115. {
  116. /* using a branch instead of a cmov,
  117. * because it's faster in scenarios where matchIdx >= idxLowLimit is generally true,
  118. * aka almost all candidates are within range */
  119. U32 mval;
  120. if (matchIdx >= idxLowLimit) {
  121. mval = MEM_read32(matchAddress);
  122. } else {
  123. mval = MEM_read32(currentPtr) ^ 1; /* guaranteed to not match. */
  124. }
  125. return (MEM_read32(currentPtr) == mval);
  126. }
  127. /**
  128. * If you squint hard enough (and ignore repcodes), the search operation at any
  129. * given position is broken into 4 stages:
  130. *
  131. * 1. Hash (map position to hash value via input read)
  132. * 2. Lookup (map hash val to index via hashtable read)
  133. * 3. Load (map index to value at that position via input read)
  134. * 4. Compare
  135. *
  136. * Each of these steps involves a memory read at an address which is computed
  137. * from the previous step. This means these steps must be sequenced and their
  138. * latencies are cumulative.
  139. *
  140. * Rather than do 1->2->3->4 sequentially for a single position before moving
  141. * onto the next, this implementation interleaves these operations across the
  142. * next few positions:
  143. *
  144. * R = Repcode Read & Compare
  145. * H = Hash
  146. * T = Table Lookup
  147. * M = Match Read & Compare
  148. *
  149. * Pos | Time -->
  150. * ----+-------------------
  151. * N | ... M
  152. * N+1 | ... TM
  153. * N+2 | R H T M
  154. * N+3 | H TM
  155. * N+4 | R H T M
  156. * N+5 | H ...
  157. * N+6 | R ...
  158. *
  159. * This is very much analogous to the pipelining of execution in a CPU. And just
  160. * like a CPU, we have to dump the pipeline when we find a match (i.e., take a
  161. * branch).
  162. *
  163. * When this happens, we throw away our current state, and do the following prep
  164. * to re-enter the loop:
  165. *
  166. * Pos | Time -->
  167. * ----+-------------------
  168. * N | H T
  169. * N+1 | H
  170. *
  171. * This is also the work we do at the beginning to enter the loop initially.
  172. */
  173. FORCE_INLINE_TEMPLATE
  174. ZSTD_ALLOW_POINTER_OVERFLOW_ATTR
  175. size_t ZSTD_compressBlock_fast_noDict_generic(
  176. ZSTD_MatchState_t* ms, SeqStore_t* seqStore, U32 rep[ZSTD_REP_NUM],
  177. void const* src, size_t srcSize,
  178. U32 const mls, int useCmov)
  179. {
  180. const ZSTD_compressionParameters* const cParams = &ms->cParams;
  181. U32* const hashTable = ms->hashTable;
  182. U32 const hlog = cParams->hashLog;
  183. size_t const stepSize = cParams->targetLength + !(cParams->targetLength) + 1; /* min 2 */
  184. const BYTE* const base = ms->window.base;
  185. const BYTE* const istart = (const BYTE*)src;
  186. const U32 endIndex = (U32)((size_t)(istart - base) + srcSize);
  187. const U32 prefixStartIndex = ZSTD_getLowestPrefixIndex(ms, endIndex, cParams->windowLog);
  188. const BYTE* const prefixStart = base + prefixStartIndex;
  189. const BYTE* const iend = istart + srcSize;
  190. const BYTE* const ilimit = iend - HASH_READ_SIZE;
  191. const BYTE* anchor = istart;
  192. const BYTE* ip0 = istart;
  193. const BYTE* ip1;
  194. const BYTE* ip2;
  195. const BYTE* ip3;
  196. U32 current0;
  197. U32 rep_offset1 = rep[0];
  198. U32 rep_offset2 = rep[1];
  199. U32 offsetSaved1 = 0, offsetSaved2 = 0;
  200. size_t hash0; /* hash for ip0 */
  201. size_t hash1; /* hash for ip1 */
  202. U32 matchIdx; /* match idx for ip0 */
  203. U32 offcode;
  204. const BYTE* match0;
  205. size_t mLength;
  206. /* ip0 and ip1 are always adjacent. The targetLength skipping and
  207. * uncompressibility acceleration is applied to every other position,
  208. * matching the behavior of #1562. step therefore represents the gap
  209. * between pairs of positions, from ip0 to ip2 or ip1 to ip3. */
  210. size_t step;
  211. const BYTE* nextStep;
  212. const size_t kStepIncr = (1 << (kSearchStrength - 1));
  213. const ZSTD_match4Found matchFound = useCmov ? ZSTD_match4Found_cmov : ZSTD_match4Found_branch;
  214. DEBUGLOG(5, "ZSTD_compressBlock_fast_generic");
  215. ip0 += (ip0 == prefixStart);
  216. { U32 const curr = (U32)(ip0 - base);
  217. U32 const windowLow = ZSTD_getLowestPrefixIndex(ms, curr, cParams->windowLog);
  218. U32 const maxRep = curr - windowLow;
  219. if (rep_offset2 > maxRep) offsetSaved2 = rep_offset2, rep_offset2 = 0;
  220. if (rep_offset1 > maxRep) offsetSaved1 = rep_offset1, rep_offset1 = 0;
  221. }
  222. /* start each op */
  223. _start: /* Requires: ip0 */
  224. step = stepSize;
  225. nextStep = ip0 + kStepIncr;
  226. /* calculate positions, ip0 - anchor == 0, so we skip step calc */
  227. ip1 = ip0 + 1;
  228. ip2 = ip0 + step;
  229. ip3 = ip2 + 1;
  230. if (ip3 >= ilimit) {
  231. goto _cleanup;
  232. }
  233. hash0 = ZSTD_hashPtr(ip0, hlog, mls);
  234. hash1 = ZSTD_hashPtr(ip1, hlog, mls);
  235. matchIdx = hashTable[hash0];
  236. do {
  237. /* load repcode match for ip[2]*/
  238. const U32 rval = MEM_read32(ip2 - rep_offset1);
  239. /* write back hash table entry */
  240. current0 = (U32)(ip0 - base);
  241. hashTable[hash0] = current0;
  242. /* check repcode at ip[2] */
  243. if ((MEM_read32(ip2) == rval) & (rep_offset1 > 0)) {
  244. ip0 = ip2;
  245. match0 = ip0 - rep_offset1;
  246. mLength = ip0[-1] == match0[-1];
  247. ip0 -= mLength;
  248. match0 -= mLength;
  249. offcode = REPCODE1_TO_OFFBASE;
  250. mLength += 4;
  251. /* Write next hash table entry: it's already calculated.
  252. * This write is known to be safe because ip1 is before the
  253. * repcode (ip2). */
  254. hashTable[hash1] = (U32)(ip1 - base);
  255. goto _match;
  256. }
  257. if (matchFound(ip0, base + matchIdx, matchIdx, prefixStartIndex)) {
  258. /* Write next hash table entry (it's already calculated).
  259. * This write is known to be safe because the ip1 == ip0 + 1,
  260. * so searching will resume after ip1 */
  261. hashTable[hash1] = (U32)(ip1 - base);
  262. goto _offset;
  263. }
  264. /* lookup ip[1] */
  265. matchIdx = hashTable[hash1];
  266. /* hash ip[2] */
  267. hash0 = hash1;
  268. hash1 = ZSTD_hashPtr(ip2, hlog, mls);
  269. /* advance to next positions */
  270. ip0 = ip1;
  271. ip1 = ip2;
  272. ip2 = ip3;
  273. /* write back hash table entry */
  274. current0 = (U32)(ip0 - base);
  275. hashTable[hash0] = current0;
  276. if (matchFound(ip0, base + matchIdx, matchIdx, prefixStartIndex)) {
  277. /* Write next hash table entry, since it's already calculated */
  278. if (step <= 4) {
  279. /* Avoid writing an index if it's >= position where search will resume.
  280. * The minimum possible match has length 4, so search can resume at ip0 + 4.
  281. */
  282. hashTable[hash1] = (U32)(ip1 - base);
  283. }
  284. goto _offset;
  285. }
  286. /* lookup ip[1] */
  287. matchIdx = hashTable[hash1];
  288. /* hash ip[2] */
  289. hash0 = hash1;
  290. hash1 = ZSTD_hashPtr(ip2, hlog, mls);
  291. /* advance to next positions */
  292. ip0 = ip1;
  293. ip1 = ip2;
  294. ip2 = ip0 + step;
  295. ip3 = ip1 + step;
  296. /* calculate step */
  297. if (ip2 >= nextStep) {
  298. step++;
  299. PREFETCH_L1(ip1 + 64);
  300. PREFETCH_L1(ip1 + 128);
  301. nextStep += kStepIncr;
  302. }
  303. } while (ip3 < ilimit);
  304. _cleanup:
  305. /* Note that there are probably still a couple positions one could search.
  306. * However, it seems to be a meaningful performance hit to try to search
  307. * them. So let's not. */
  308. /* When the repcodes are outside of the prefix, we set them to zero before the loop.
  309. * When the offsets are still zero, we need to restore them after the block to have a correct
  310. * repcode history. If only one offset was invalid, it is easy. The tricky case is when both
  311. * offsets were invalid. We need to figure out which offset to refill with.
  312. * - If both offsets are zero they are in the same order.
  313. * - If both offsets are non-zero, we won't restore the offsets from `offsetSaved[12]`.
  314. * - If only one is zero, we need to decide which offset to restore.
  315. * - If rep_offset1 is non-zero, then rep_offset2 must be offsetSaved1.
  316. * - It is impossible for rep_offset2 to be non-zero.
  317. *
  318. * So if rep_offset1 started invalid (offsetSaved1 != 0) and became valid (rep_offset1 != 0), then
  319. * set rep[0] = rep_offset1 and rep[1] = offsetSaved1.
  320. */
  321. offsetSaved2 = ((offsetSaved1 != 0) && (rep_offset1 != 0)) ? offsetSaved1 : offsetSaved2;
  322. /* save reps for next block */
  323. rep[0] = rep_offset1 ? rep_offset1 : offsetSaved1;
  324. rep[1] = rep_offset2 ? rep_offset2 : offsetSaved2;
  325. /* Return the last literals size */
  326. return (size_t)(iend - anchor);
  327. _offset: /* Requires: ip0, idx */
  328. /* Compute the offset code. */
  329. match0 = base + matchIdx;
  330. rep_offset2 = rep_offset1;
  331. rep_offset1 = (U32)(ip0-match0);
  332. offcode = OFFSET_TO_OFFBASE(rep_offset1);
  333. mLength = 4;
  334. /* Count the backwards match length. */
  335. while (((ip0>anchor) & (match0>prefixStart)) && (ip0[-1] == match0[-1])) {
  336. ip0--;
  337. match0--;
  338. mLength++;
  339. }
  340. _match: /* Requires: ip0, match0, offcode */
  341. /* Count the forward length. */
  342. mLength += ZSTD_count(ip0 + mLength, match0 + mLength, iend);
  343. ZSTD_storeSeq(seqStore, (size_t)(ip0 - anchor), anchor, iend, offcode, mLength);
  344. ip0 += mLength;
  345. anchor = ip0;
  346. /* Fill table and check for immediate repcode. */
  347. if (ip0 <= ilimit) {
  348. /* Fill Table */
  349. assert(base+current0+2 > istart); /* check base overflow */
  350. hashTable[ZSTD_hashPtr(base+current0+2, hlog, mls)] = current0+2; /* here because current+2 could be > iend-8 */
  351. hashTable[ZSTD_hashPtr(ip0-2, hlog, mls)] = (U32)(ip0-2-base);
  352. if (rep_offset2 > 0) { /* rep_offset2==0 means rep_offset2 is invalidated */
  353. while ( (ip0 <= ilimit) && (MEM_read32(ip0) == MEM_read32(ip0 - rep_offset2)) ) {
  354. /* store sequence */
  355. size_t const rLength = ZSTD_count(ip0+4, ip0+4-rep_offset2, iend) + 4;
  356. { U32 const tmpOff = rep_offset2; rep_offset2 = rep_offset1; rep_offset1 = tmpOff; } /* swap rep_offset2 <=> rep_offset1 */
  357. hashTable[ZSTD_hashPtr(ip0, hlog, mls)] = (U32)(ip0-base);
  358. ip0 += rLength;
  359. ZSTD_storeSeq(seqStore, 0 /*litLen*/, anchor, iend, REPCODE1_TO_OFFBASE, rLength);
  360. anchor = ip0;
  361. continue; /* faster when present (confirmed on gcc-8) ... (?) */
  362. } } }
  363. goto _start;
  364. }
  365. #define ZSTD_GEN_FAST_FN(dictMode, mml, cmov) \
  366. static size_t ZSTD_compressBlock_fast_##dictMode##_##mml##_##cmov( \
  367. ZSTD_MatchState_t* ms, SeqStore_t* seqStore, U32 rep[ZSTD_REP_NUM], \
  368. void const* src, size_t srcSize) \
  369. { \
  370. return ZSTD_compressBlock_fast_##dictMode##_generic(ms, seqStore, rep, src, srcSize, mml, cmov); \
  371. }
  372. ZSTD_GEN_FAST_FN(noDict, 4, 1)
  373. ZSTD_GEN_FAST_FN(noDict, 5, 1)
  374. ZSTD_GEN_FAST_FN(noDict, 6, 1)
  375. ZSTD_GEN_FAST_FN(noDict, 7, 1)
  376. ZSTD_GEN_FAST_FN(noDict, 4, 0)
  377. ZSTD_GEN_FAST_FN(noDict, 5, 0)
  378. ZSTD_GEN_FAST_FN(noDict, 6, 0)
  379. ZSTD_GEN_FAST_FN(noDict, 7, 0)
  380. size_t ZSTD_compressBlock_fast(
  381. ZSTD_MatchState_t* ms, SeqStore_t* seqStore, U32 rep[ZSTD_REP_NUM],
  382. void const* src, size_t srcSize)
  383. {
  384. U32 const mml = ms->cParams.minMatch;
  385. /* use cmov when "candidate in range" branch is likely unpredictable */
  386. int const useCmov = ms->cParams.windowLog < 19;
  387. assert(ms->dictMatchState == NULL);
  388. if (useCmov) {
  389. switch(mml)
  390. {
  391. default: /* includes case 3 */
  392. case 4 :
  393. return ZSTD_compressBlock_fast_noDict_4_1(ms, seqStore, rep, src, srcSize);
  394. case 5 :
  395. return ZSTD_compressBlock_fast_noDict_5_1(ms, seqStore, rep, src, srcSize);
  396. case 6 :
  397. return ZSTD_compressBlock_fast_noDict_6_1(ms, seqStore, rep, src, srcSize);
  398. case 7 :
  399. return ZSTD_compressBlock_fast_noDict_7_1(ms, seqStore, rep, src, srcSize);
  400. }
  401. } else {
  402. /* use a branch instead */
  403. switch(mml)
  404. {
  405. default: /* includes case 3 */
  406. case 4 :
  407. return ZSTD_compressBlock_fast_noDict_4_0(ms, seqStore, rep, src, srcSize);
  408. case 5 :
  409. return ZSTD_compressBlock_fast_noDict_5_0(ms, seqStore, rep, src, srcSize);
  410. case 6 :
  411. return ZSTD_compressBlock_fast_noDict_6_0(ms, seqStore, rep, src, srcSize);
  412. case 7 :
  413. return ZSTD_compressBlock_fast_noDict_7_0(ms, seqStore, rep, src, srcSize);
  414. }
  415. }
  416. }
  417. FORCE_INLINE_TEMPLATE
  418. ZSTD_ALLOW_POINTER_OVERFLOW_ATTR
  419. size_t ZSTD_compressBlock_fast_dictMatchState_generic(
  420. ZSTD_MatchState_t* ms, SeqStore_t* seqStore, U32 rep[ZSTD_REP_NUM],
  421. void const* src, size_t srcSize, U32 const mls, U32 const hasStep)
  422. {
  423. const ZSTD_compressionParameters* const cParams = &ms->cParams;
  424. U32* const hashTable = ms->hashTable;
  425. U32 const hlog = cParams->hashLog;
  426. /* support stepSize of 0 */
  427. U32 const stepSize = cParams->targetLength + !(cParams->targetLength);
  428. const BYTE* const base = ms->window.base;
  429. const BYTE* const istart = (const BYTE*)src;
  430. const BYTE* ip0 = istart;
  431. const BYTE* ip1 = ip0 + stepSize; /* we assert below that stepSize >= 1 */
  432. const BYTE* anchor = istart;
  433. const U32 prefixStartIndex = ms->window.dictLimit;
  434. const BYTE* const prefixStart = base + prefixStartIndex;
  435. const BYTE* const iend = istart + srcSize;
  436. const BYTE* const ilimit = iend - HASH_READ_SIZE;
  437. U32 offset_1=rep[0], offset_2=rep[1];
  438. const ZSTD_MatchState_t* const dms = ms->dictMatchState;
  439. const ZSTD_compressionParameters* const dictCParams = &dms->cParams ;
  440. const U32* const dictHashTable = dms->hashTable;
  441. const U32 dictStartIndex = dms->window.dictLimit;
  442. const BYTE* const dictBase = dms->window.base;
  443. const BYTE* const dictStart = dictBase + dictStartIndex;
  444. const BYTE* const dictEnd = dms->window.nextSrc;
  445. const U32 dictIndexDelta = prefixStartIndex - (U32)(dictEnd - dictBase);
  446. const U32 dictAndPrefixLength = (U32)(istart - prefixStart + dictEnd - dictStart);
  447. const U32 dictHBits = dictCParams->hashLog + ZSTD_SHORT_CACHE_TAG_BITS;
  448. /* if a dictionary is still attached, it necessarily means that
  449. * it is within window size. So we just check it. */
  450. const U32 maxDistance = 1U << cParams->windowLog;
  451. const U32 endIndex = (U32)((size_t)(istart - base) + srcSize);
  452. assert(endIndex - prefixStartIndex <= maxDistance);
  453. (void)maxDistance; (void)endIndex; /* these variables are not used when assert() is disabled */
  454. (void)hasStep; /* not currently specialized on whether it's accelerated */
  455. /* ensure there will be no underflow
  456. * when translating a dict index into a local index */
  457. assert(prefixStartIndex >= (U32)(dictEnd - dictBase));
  458. if (ms->prefetchCDictTables) {
  459. size_t const hashTableBytes = (((size_t)1) << dictCParams->hashLog) * sizeof(U32);
  460. PREFETCH_AREA(dictHashTable, hashTableBytes);
  461. }
  462. /* init */
  463. DEBUGLOG(5, "ZSTD_compressBlock_fast_dictMatchState_generic");
  464. ip0 += (dictAndPrefixLength == 0);
  465. /* dictMatchState repCode checks don't currently handle repCode == 0
  466. * disabling. */
  467. assert(offset_1 <= dictAndPrefixLength);
  468. assert(offset_2 <= dictAndPrefixLength);
  469. /* Outer search loop */
  470. assert(stepSize >= 1);
  471. while (ip1 <= ilimit) { /* repcode check at (ip0 + 1) is safe because ip0 < ip1 */
  472. size_t mLength;
  473. size_t hash0 = ZSTD_hashPtr(ip0, hlog, mls);
  474. size_t const dictHashAndTag0 = ZSTD_hashPtr(ip0, dictHBits, mls);
  475. U32 dictMatchIndexAndTag = dictHashTable[dictHashAndTag0 >> ZSTD_SHORT_CACHE_TAG_BITS];
  476. int dictTagsMatch = ZSTD_comparePackedTags(dictMatchIndexAndTag, dictHashAndTag0);
  477. U32 matchIndex = hashTable[hash0];
  478. U32 curr = (U32)(ip0 - base);
  479. size_t step = stepSize;
  480. const size_t kStepIncr = 1 << kSearchStrength;
  481. const BYTE* nextStep = ip0 + kStepIncr;
  482. /* Inner search loop */
  483. while (1) {
  484. const BYTE* match = base + matchIndex;
  485. const U32 repIndex = curr + 1 - offset_1;
  486. const BYTE* repMatch = (repIndex < prefixStartIndex) ?
  487. dictBase + (repIndex - dictIndexDelta) :
  488. base + repIndex;
  489. const size_t hash1 = ZSTD_hashPtr(ip1, hlog, mls);
  490. size_t const dictHashAndTag1 = ZSTD_hashPtr(ip1, dictHBits, mls);
  491. hashTable[hash0] = curr; /* update hash table */
  492. if ((ZSTD_index_overlap_check(prefixStartIndex, repIndex))
  493. && (MEM_read32(repMatch) == MEM_read32(ip0 + 1))) {
  494. const BYTE* const repMatchEnd = repIndex < prefixStartIndex ? dictEnd : iend;
  495. mLength = ZSTD_count_2segments(ip0 + 1 + 4, repMatch + 4, iend, repMatchEnd, prefixStart) + 4;
  496. ip0++;
  497. ZSTD_storeSeq(seqStore, (size_t) (ip0 - anchor), anchor, iend, REPCODE1_TO_OFFBASE, mLength);
  498. break;
  499. }
  500. if (dictTagsMatch) {
  501. /* Found a possible dict match */
  502. const U32 dictMatchIndex = dictMatchIndexAndTag >> ZSTD_SHORT_CACHE_TAG_BITS;
  503. const BYTE* dictMatch = dictBase + dictMatchIndex;
  504. if (dictMatchIndex > dictStartIndex &&
  505. MEM_read32(dictMatch) == MEM_read32(ip0)) {
  506. /* To replicate extDict parse behavior, we only use dict matches when the normal matchIndex is invalid */
  507. if (matchIndex <= prefixStartIndex) {
  508. U32 const offset = (U32) (curr - dictMatchIndex - dictIndexDelta);
  509. mLength = ZSTD_count_2segments(ip0 + 4, dictMatch + 4, iend, dictEnd, prefixStart) + 4;
  510. while (((ip0 > anchor) & (dictMatch > dictStart))
  511. && (ip0[-1] == dictMatch[-1])) {
  512. ip0--;
  513. dictMatch--;
  514. mLength++;
  515. } /* catch up */
  516. offset_2 = offset_1;
  517. offset_1 = offset;
  518. ZSTD_storeSeq(seqStore, (size_t) (ip0 - anchor), anchor, iend, OFFSET_TO_OFFBASE(offset), mLength);
  519. break;
  520. }
  521. }
  522. }
  523. if (ZSTD_match4Found_cmov(ip0, match, matchIndex, prefixStartIndex)) {
  524. /* found a regular match of size >= 4 */
  525. U32 const offset = (U32) (ip0 - match);
  526. mLength = ZSTD_count(ip0 + 4, match + 4, iend) + 4;
  527. while (((ip0 > anchor) & (match > prefixStart))
  528. && (ip0[-1] == match[-1])) {
  529. ip0--;
  530. match--;
  531. mLength++;
  532. } /* catch up */
  533. offset_2 = offset_1;
  534. offset_1 = offset;
  535. ZSTD_storeSeq(seqStore, (size_t) (ip0 - anchor), anchor, iend, OFFSET_TO_OFFBASE(offset), mLength);
  536. break;
  537. }
  538. /* Prepare for next iteration */
  539. dictMatchIndexAndTag = dictHashTable[dictHashAndTag1 >> ZSTD_SHORT_CACHE_TAG_BITS];
  540. dictTagsMatch = ZSTD_comparePackedTags(dictMatchIndexAndTag, dictHashAndTag1);
  541. matchIndex = hashTable[hash1];
  542. if (ip1 >= nextStep) {
  543. step++;
  544. nextStep += kStepIncr;
  545. }
  546. ip0 = ip1;
  547. ip1 = ip1 + step;
  548. if (ip1 > ilimit) goto _cleanup;
  549. curr = (U32)(ip0 - base);
  550. hash0 = hash1;
  551. } /* end inner search loop */
  552. /* match found */
  553. assert(mLength);
  554. ip0 += mLength;
  555. anchor = ip0;
  556. if (ip0 <= ilimit) {
  557. /* Fill Table */
  558. assert(base+curr+2 > istart); /* check base overflow */
  559. hashTable[ZSTD_hashPtr(base+curr+2, hlog, mls)] = curr+2; /* here because curr+2 could be > iend-8 */
  560. hashTable[ZSTD_hashPtr(ip0-2, hlog, mls)] = (U32)(ip0-2-base);
  561. /* check immediate repcode */
  562. while (ip0 <= ilimit) {
  563. U32 const current2 = (U32)(ip0-base);
  564. U32 const repIndex2 = current2 - offset_2;
  565. const BYTE* repMatch2 = repIndex2 < prefixStartIndex ?
  566. dictBase - dictIndexDelta + repIndex2 :
  567. base + repIndex2;
  568. if ( (ZSTD_index_overlap_check(prefixStartIndex, repIndex2))
  569. && (MEM_read32(repMatch2) == MEM_read32(ip0))) {
  570. const BYTE* const repEnd2 = repIndex2 < prefixStartIndex ? dictEnd : iend;
  571. size_t const repLength2 = ZSTD_count_2segments(ip0+4, repMatch2+4, iend, repEnd2, prefixStart) + 4;
  572. U32 tmpOffset = offset_2; offset_2 = offset_1; offset_1 = tmpOffset; /* swap offset_2 <=> offset_1 */
  573. ZSTD_storeSeq(seqStore, 0, anchor, iend, REPCODE1_TO_OFFBASE, repLength2);
  574. hashTable[ZSTD_hashPtr(ip0, hlog, mls)] = current2;
  575. ip0 += repLength2;
  576. anchor = ip0;
  577. continue;
  578. }
  579. break;
  580. }
  581. }
  582. /* Prepare for next iteration */
  583. assert(ip0 == anchor);
  584. ip1 = ip0 + stepSize;
  585. }
  586. _cleanup:
  587. /* save reps for next block */
  588. rep[0] = offset_1;
  589. rep[1] = offset_2;
  590. /* Return the last literals size */
  591. return (size_t)(iend - anchor);
  592. }
  593. ZSTD_GEN_FAST_FN(dictMatchState, 4, 0)
  594. ZSTD_GEN_FAST_FN(dictMatchState, 5, 0)
  595. ZSTD_GEN_FAST_FN(dictMatchState, 6, 0)
  596. ZSTD_GEN_FAST_FN(dictMatchState, 7, 0)
  597. size_t ZSTD_compressBlock_fast_dictMatchState(
  598. ZSTD_MatchState_t* ms, SeqStore_t* seqStore, U32 rep[ZSTD_REP_NUM],
  599. void const* src, size_t srcSize)
  600. {
  601. U32 const mls = ms->cParams.minMatch;
  602. assert(ms->dictMatchState != NULL);
  603. switch(mls)
  604. {
  605. default: /* includes case 3 */
  606. case 4 :
  607. return ZSTD_compressBlock_fast_dictMatchState_4_0(ms, seqStore, rep, src, srcSize);
  608. case 5 :
  609. return ZSTD_compressBlock_fast_dictMatchState_5_0(ms, seqStore, rep, src, srcSize);
  610. case 6 :
  611. return ZSTD_compressBlock_fast_dictMatchState_6_0(ms, seqStore, rep, src, srcSize);
  612. case 7 :
  613. return ZSTD_compressBlock_fast_dictMatchState_7_0(ms, seqStore, rep, src, srcSize);
  614. }
  615. }
  616. static
  617. ZSTD_ALLOW_POINTER_OVERFLOW_ATTR
  618. size_t ZSTD_compressBlock_fast_extDict_generic(
  619. ZSTD_MatchState_t* ms, SeqStore_t* seqStore, U32 rep[ZSTD_REP_NUM],
  620. void const* src, size_t srcSize, U32 const mls, U32 const hasStep)
  621. {
  622. const ZSTD_compressionParameters* const cParams = &ms->cParams;
  623. U32* const hashTable = ms->hashTable;
  624. U32 const hlog = cParams->hashLog;
  625. /* support stepSize of 0 */
  626. size_t const stepSize = cParams->targetLength + !(cParams->targetLength) + 1;
  627. const BYTE* const base = ms->window.base;
  628. const BYTE* const dictBase = ms->window.dictBase;
  629. const BYTE* const istart = (const BYTE*)src;
  630. const BYTE* anchor = istart;
  631. const U32 endIndex = (U32)((size_t)(istart - base) + srcSize);
  632. const U32 lowLimit = ZSTD_getLowestMatchIndex(ms, endIndex, cParams->windowLog);
  633. const U32 dictStartIndex = lowLimit;
  634. const BYTE* const dictStart = dictBase + dictStartIndex;
  635. const U32 dictLimit = ms->window.dictLimit;
  636. const U32 prefixStartIndex = dictLimit < lowLimit ? lowLimit : dictLimit;
  637. const BYTE* const prefixStart = base + prefixStartIndex;
  638. const BYTE* const dictEnd = dictBase + prefixStartIndex;
  639. const BYTE* const iend = istart + srcSize;
  640. const BYTE* const ilimit = iend - 8;
  641. U32 offset_1=rep[0], offset_2=rep[1];
  642. U32 offsetSaved1 = 0, offsetSaved2 = 0;
  643. const BYTE* ip0 = istart;
  644. const BYTE* ip1;
  645. const BYTE* ip2;
  646. const BYTE* ip3;
  647. U32 current0;
  648. size_t hash0; /* hash for ip0 */
  649. size_t hash1; /* hash for ip1 */
  650. U32 idx; /* match idx for ip0 */
  651. const BYTE* idxBase; /* base pointer for idx */
  652. U32 offcode;
  653. const BYTE* match0;
  654. size_t mLength;
  655. const BYTE* matchEnd = 0; /* initialize to avoid warning, assert != 0 later */
  656. size_t step;
  657. const BYTE* nextStep;
  658. const size_t kStepIncr = (1 << (kSearchStrength - 1));
  659. (void)hasStep; /* not currently specialized on whether it's accelerated */
  660. DEBUGLOG(5, "ZSTD_compressBlock_fast_extDict_generic (offset_1=%u)", offset_1);
  661. /* switch to "regular" variant if extDict is invalidated due to maxDistance */
  662. if (prefixStartIndex == dictStartIndex)
  663. return ZSTD_compressBlock_fast(ms, seqStore, rep, src, srcSize);
  664. { U32 const curr = (U32)(ip0 - base);
  665. U32 const maxRep = curr - dictStartIndex;
  666. if (offset_2 >= maxRep) offsetSaved2 = offset_2, offset_2 = 0;
  667. if (offset_1 >= maxRep) offsetSaved1 = offset_1, offset_1 = 0;
  668. }
  669. /* start each op */
  670. _start: /* Requires: ip0 */
  671. step = stepSize;
  672. nextStep = ip0 + kStepIncr;
  673. /* calculate positions, ip0 - anchor == 0, so we skip step calc */
  674. ip1 = ip0 + 1;
  675. ip2 = ip0 + step;
  676. ip3 = ip2 + 1;
  677. if (ip3 >= ilimit) {
  678. goto _cleanup;
  679. }
  680. hash0 = ZSTD_hashPtr(ip0, hlog, mls);
  681. hash1 = ZSTD_hashPtr(ip1, hlog, mls);
  682. idx = hashTable[hash0];
  683. idxBase = idx < prefixStartIndex ? dictBase : base;
  684. do {
  685. { /* load repcode match for ip[2] */
  686. U32 const current2 = (U32)(ip2 - base);
  687. U32 const repIndex = current2 - offset_1;
  688. const BYTE* const repBase = repIndex < prefixStartIndex ? dictBase : base;
  689. U32 rval;
  690. if ( ((U32)(prefixStartIndex - repIndex) >= 4) /* intentional underflow */
  691. & (offset_1 > 0) ) {
  692. rval = MEM_read32(repBase + repIndex);
  693. } else {
  694. rval = MEM_read32(ip2) ^ 1; /* guaranteed to not match. */
  695. }
  696. /* write back hash table entry */
  697. current0 = (U32)(ip0 - base);
  698. hashTable[hash0] = current0;
  699. /* check repcode at ip[2] */
  700. if (MEM_read32(ip2) == rval) {
  701. ip0 = ip2;
  702. match0 = repBase + repIndex;
  703. matchEnd = repIndex < prefixStartIndex ? dictEnd : iend;
  704. assert((match0 != prefixStart) & (match0 != dictStart));
  705. mLength = ip0[-1] == match0[-1];
  706. ip0 -= mLength;
  707. match0 -= mLength;
  708. offcode = REPCODE1_TO_OFFBASE;
  709. mLength += 4;
  710. goto _match;
  711. } }
  712. { /* load match for ip[0] */
  713. U32 const mval = idx >= dictStartIndex ?
  714. MEM_read32(idxBase + idx) :
  715. MEM_read32(ip0) ^ 1; /* guaranteed not to match */
  716. /* check match at ip[0] */
  717. if (MEM_read32(ip0) == mval) {
  718. /* found a match! */
  719. goto _offset;
  720. } }
  721. /* lookup ip[1] */
  722. idx = hashTable[hash1];
  723. idxBase = idx < prefixStartIndex ? dictBase : base;
  724. /* hash ip[2] */
  725. hash0 = hash1;
  726. hash1 = ZSTD_hashPtr(ip2, hlog, mls);
  727. /* advance to next positions */
  728. ip0 = ip1;
  729. ip1 = ip2;
  730. ip2 = ip3;
  731. /* write back hash table entry */
  732. current0 = (U32)(ip0 - base);
  733. hashTable[hash0] = current0;
  734. { /* load match for ip[0] */
  735. U32 const mval = idx >= dictStartIndex ?
  736. MEM_read32(idxBase + idx) :
  737. MEM_read32(ip0) ^ 1; /* guaranteed not to match */
  738. /* check match at ip[0] */
  739. if (MEM_read32(ip0) == mval) {
  740. /* found a match! */
  741. goto _offset;
  742. } }
  743. /* lookup ip[1] */
  744. idx = hashTable[hash1];
  745. idxBase = idx < prefixStartIndex ? dictBase : base;
  746. /* hash ip[2] */
  747. hash0 = hash1;
  748. hash1 = ZSTD_hashPtr(ip2, hlog, mls);
  749. /* advance to next positions */
  750. ip0 = ip1;
  751. ip1 = ip2;
  752. ip2 = ip0 + step;
  753. ip3 = ip1 + step;
  754. /* calculate step */
  755. if (ip2 >= nextStep) {
  756. step++;
  757. PREFETCH_L1(ip1 + 64);
  758. PREFETCH_L1(ip1 + 128);
  759. nextStep += kStepIncr;
  760. }
  761. } while (ip3 < ilimit);
  762. _cleanup:
  763. /* Note that there are probably still a couple positions we could search.
  764. * However, it seems to be a meaningful performance hit to try to search
  765. * them. So let's not. */
  766. /* If offset_1 started invalid (offsetSaved1 != 0) and became valid (offset_1 != 0),
  767. * rotate saved offsets. See comment in ZSTD_compressBlock_fast_noDict for more context. */
  768. offsetSaved2 = ((offsetSaved1 != 0) && (offset_1 != 0)) ? offsetSaved1 : offsetSaved2;
  769. /* save reps for next block */
  770. rep[0] = offset_1 ? offset_1 : offsetSaved1;
  771. rep[1] = offset_2 ? offset_2 : offsetSaved2;
  772. /* Return the last literals size */
  773. return (size_t)(iend - anchor);
  774. _offset: /* Requires: ip0, idx, idxBase */
  775. /* Compute the offset code. */
  776. { U32 const offset = current0 - idx;
  777. const BYTE* const lowMatchPtr = idx < prefixStartIndex ? dictStart : prefixStart;
  778. matchEnd = idx < prefixStartIndex ? dictEnd : iend;
  779. match0 = idxBase + idx;
  780. offset_2 = offset_1;
  781. offset_1 = offset;
  782. offcode = OFFSET_TO_OFFBASE(offset);
  783. mLength = 4;
  784. /* Count the backwards match length. */
  785. while (((ip0>anchor) & (match0>lowMatchPtr)) && (ip0[-1] == match0[-1])) {
  786. ip0--;
  787. match0--;
  788. mLength++;
  789. } }
  790. _match: /* Requires: ip0, match0, offcode, matchEnd */
  791. /* Count the forward length. */
  792. assert(matchEnd != 0);
  793. mLength += ZSTD_count_2segments(ip0 + mLength, match0 + mLength, iend, matchEnd, prefixStart);
  794. ZSTD_storeSeq(seqStore, (size_t)(ip0 - anchor), anchor, iend, offcode, mLength);
  795. ip0 += mLength;
  796. anchor = ip0;
  797. /* write next hash table entry */
  798. if (ip1 < ip0) {
  799. hashTable[hash1] = (U32)(ip1 - base);
  800. }
  801. /* Fill table and check for immediate repcode. */
  802. if (ip0 <= ilimit) {
  803. /* Fill Table */
  804. assert(base+current0+2 > istart); /* check base overflow */
  805. hashTable[ZSTD_hashPtr(base+current0+2, hlog, mls)] = current0+2; /* here because current+2 could be > iend-8 */
  806. hashTable[ZSTD_hashPtr(ip0-2, hlog, mls)] = (U32)(ip0-2-base);
  807. while (ip0 <= ilimit) {
  808. U32 const repIndex2 = (U32)(ip0-base) - offset_2;
  809. const BYTE* const repMatch2 = repIndex2 < prefixStartIndex ? dictBase + repIndex2 : base + repIndex2;
  810. if ( ((ZSTD_index_overlap_check(prefixStartIndex, repIndex2)) & (offset_2 > 0))
  811. && (MEM_read32(repMatch2) == MEM_read32(ip0)) ) {
  812. const BYTE* const repEnd2 = repIndex2 < prefixStartIndex ? dictEnd : iend;
  813. size_t const repLength2 = ZSTD_count_2segments(ip0+4, repMatch2+4, iend, repEnd2, prefixStart) + 4;
  814. { U32 const tmpOffset = offset_2; offset_2 = offset_1; offset_1 = tmpOffset; } /* swap offset_2 <=> offset_1 */
  815. ZSTD_storeSeq(seqStore, 0 /*litlen*/, anchor, iend, REPCODE1_TO_OFFBASE, repLength2);
  816. hashTable[ZSTD_hashPtr(ip0, hlog, mls)] = (U32)(ip0-base);
  817. ip0 += repLength2;
  818. anchor = ip0;
  819. continue;
  820. }
  821. break;
  822. } }
  823. goto _start;
  824. }
  825. ZSTD_GEN_FAST_FN(extDict, 4, 0)
  826. ZSTD_GEN_FAST_FN(extDict, 5, 0)
  827. ZSTD_GEN_FAST_FN(extDict, 6, 0)
  828. ZSTD_GEN_FAST_FN(extDict, 7, 0)
  829. size_t ZSTD_compressBlock_fast_extDict(
  830. ZSTD_MatchState_t* ms, SeqStore_t* seqStore, U32 rep[ZSTD_REP_NUM],
  831. void const* src, size_t srcSize)
  832. {
  833. U32 const mls = ms->cParams.minMatch;
  834. assert(ms->dictMatchState == NULL);
  835. switch(mls)
  836. {
  837. default: /* includes case 3 */
  838. case 4 :
  839. return ZSTD_compressBlock_fast_extDict_4_0(ms, seqStore, rep, src, srcSize);
  840. case 5 :
  841. return ZSTD_compressBlock_fast_extDict_5_0(ms, seqStore, rep, src, srcSize);
  842. case 6 :
  843. return ZSTD_compressBlock_fast_extDict_6_0(ms, seqStore, rep, src, srcSize);
  844. case 7 :
  845. return ZSTD_compressBlock_fast_extDict_7_0(ms, seqStore, rep, src, srcSize);
  846. }
  847. }