You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

zstd_fast.c 36KB

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