zstd_double_fast.c 34 KB

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