zstd_fast.c 36 KB

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