zstd_double_fast.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. /*
  2. * Copyright (c) 2016-present, Yann Collet, Facebook, Inc.
  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. void ZSTD_fillDoubleHashTable(ZSTD_matchState_t* ms,
  13. ZSTD_compressionParameters const* cParams,
  14. void const* end)
  15. {
  16. U32* const hashLarge = ms->hashTable;
  17. U32 const hBitsL = cParams->hashLog;
  18. U32 const mls = cParams->searchLength;
  19. U32* const hashSmall = ms->chainTable;
  20. U32 const hBitsS = cParams->chainLog;
  21. const BYTE* const base = ms->window.base;
  22. const BYTE* ip = base + ms->nextToUpdate;
  23. const BYTE* const iend = ((const BYTE*)end) - HASH_READ_SIZE;
  24. const U32 fastHashFillStep = 3;
  25. /* Always insert every fastHashFillStep position into the hash tables.
  26. * Insert the other positions into the large hash table if their entry
  27. * is empty.
  28. */
  29. for (; ip + fastHashFillStep - 1 <= iend; ip += fastHashFillStep) {
  30. U32 const current = (U32)(ip - base);
  31. U32 i;
  32. for (i = 0; i < fastHashFillStep; ++i) {
  33. size_t const smHash = ZSTD_hashPtr(ip + i, hBitsS, mls);
  34. size_t const lgHash = ZSTD_hashPtr(ip + i, hBitsL, 8);
  35. if (i == 0)
  36. hashSmall[smHash] = current + i;
  37. if (i == 0 || hashLarge[lgHash] == 0)
  38. hashLarge[lgHash] = current + i;
  39. }
  40. }
  41. }
  42. FORCE_INLINE_TEMPLATE
  43. size_t ZSTD_compressBlock_doubleFast_generic(
  44. ZSTD_matchState_t* ms, seqStore_t* seqStore, U32 rep[ZSTD_REP_NUM],
  45. ZSTD_compressionParameters const* cParams, void const* src, size_t srcSize,
  46. U32 const mls /* template */)
  47. {
  48. U32* const hashLong = ms->hashTable;
  49. const U32 hBitsL = cParams->hashLog;
  50. U32* const hashSmall = ms->chainTable;
  51. const U32 hBitsS = cParams->chainLog;
  52. const BYTE* const base = ms->window.base;
  53. const BYTE* const istart = (const BYTE*)src;
  54. const BYTE* ip = istart;
  55. const BYTE* anchor = istart;
  56. const U32 lowestIndex = ms->window.dictLimit;
  57. const BYTE* const lowest = base + lowestIndex;
  58. const BYTE* const iend = istart + srcSize;
  59. const BYTE* const ilimit = iend - HASH_READ_SIZE;
  60. U32 offset_1=rep[0], offset_2=rep[1];
  61. U32 offsetSaved = 0;
  62. /* init */
  63. ip += (ip==lowest);
  64. { U32 const maxRep = (U32)(ip-lowest);
  65. if (offset_2 > maxRep) offsetSaved = offset_2, offset_2 = 0;
  66. if (offset_1 > maxRep) offsetSaved = offset_1, offset_1 = 0;
  67. }
  68. /* Main Search Loop */
  69. while (ip < ilimit) { /* < instead of <=, because repcode check at (ip+1) */
  70. size_t mLength;
  71. size_t const h2 = ZSTD_hashPtr(ip, hBitsL, 8);
  72. size_t const h = ZSTD_hashPtr(ip, hBitsS, mls);
  73. U32 const current = (U32)(ip-base);
  74. U32 const matchIndexL = hashLong[h2];
  75. U32 const matchIndexS = hashSmall[h];
  76. const BYTE* matchLong = base + matchIndexL;
  77. const BYTE* match = base + matchIndexS;
  78. hashLong[h2] = hashSmall[h] = current; /* update hash tables */
  79. assert(offset_1 <= current); /* supposed guaranteed by construction */
  80. if ((offset_1 > 0) & (MEM_read32(ip+1-offset_1) == MEM_read32(ip+1))) {
  81. /* favor repcode */
  82. mLength = ZSTD_count(ip+1+4, ip+1+4-offset_1, iend) + 4;
  83. ip++;
  84. ZSTD_storeSeq(seqStore, ip-anchor, anchor, 0, mLength-MINMATCH);
  85. } else {
  86. U32 offset;
  87. if ( (matchIndexL > lowestIndex) && (MEM_read64(matchLong) == MEM_read64(ip)) ) {
  88. mLength = ZSTD_count(ip+8, matchLong+8, iend) + 8;
  89. offset = (U32)(ip-matchLong);
  90. while (((ip>anchor) & (matchLong>lowest)) && (ip[-1] == matchLong[-1])) { ip--; matchLong--; mLength++; } /* catch up */
  91. } else if ( (matchIndexS > lowestIndex) && (MEM_read32(match) == MEM_read32(ip)) ) {
  92. size_t const hl3 = ZSTD_hashPtr(ip+1, hBitsL, 8);
  93. U32 const matchIndexL3 = hashLong[hl3];
  94. const BYTE* matchL3 = base + matchIndexL3;
  95. hashLong[hl3] = current + 1;
  96. if ( (matchIndexL3 > lowestIndex) && (MEM_read64(matchL3) == MEM_read64(ip+1)) ) {
  97. mLength = ZSTD_count(ip+9, matchL3+8, iend) + 8;
  98. ip++;
  99. offset = (U32)(ip-matchL3);
  100. while (((ip>anchor) & (matchL3>lowest)) && (ip[-1] == matchL3[-1])) { ip--; matchL3--; mLength++; } /* catch up */
  101. } else {
  102. mLength = ZSTD_count(ip+4, match+4, iend) + 4;
  103. offset = (U32)(ip-match);
  104. while (((ip>anchor) & (match>lowest)) && (ip[-1] == match[-1])) { ip--; match--; mLength++; } /* catch up */
  105. }
  106. } else {
  107. ip += ((ip-anchor) >> kSearchStrength) + 1;
  108. continue;
  109. }
  110. offset_2 = offset_1;
  111. offset_1 = offset;
  112. ZSTD_storeSeq(seqStore, ip-anchor, anchor, offset + ZSTD_REP_MOVE, mLength-MINMATCH);
  113. }
  114. /* match found */
  115. ip += mLength;
  116. anchor = ip;
  117. if (ip <= ilimit) {
  118. /* Fill Table */
  119. hashLong[ZSTD_hashPtr(base+current+2, hBitsL, 8)] =
  120. hashSmall[ZSTD_hashPtr(base+current+2, hBitsS, mls)] = current+2; /* here because current+2 could be > iend-8 */
  121. hashLong[ZSTD_hashPtr(ip-2, hBitsL, 8)] =
  122. hashSmall[ZSTD_hashPtr(ip-2, hBitsS, mls)] = (U32)(ip-2-base);
  123. /* check immediate repcode */
  124. while ( (ip <= ilimit)
  125. && ( (offset_2>0)
  126. & (MEM_read32(ip) == MEM_read32(ip - offset_2)) )) {
  127. /* store sequence */
  128. size_t const rLength = ZSTD_count(ip+4, ip+4-offset_2, iend) + 4;
  129. { U32 const tmpOff = offset_2; offset_2 = offset_1; offset_1 = tmpOff; } /* swap offset_2 <=> offset_1 */
  130. hashSmall[ZSTD_hashPtr(ip, hBitsS, mls)] = (U32)(ip-base);
  131. hashLong[ZSTD_hashPtr(ip, hBitsL, 8)] = (U32)(ip-base);
  132. ZSTD_storeSeq(seqStore, 0, anchor, 0, rLength-MINMATCH);
  133. ip += rLength;
  134. anchor = ip;
  135. continue; /* faster when present ... (?) */
  136. } } }
  137. /* save reps for next block */
  138. rep[0] = offset_1 ? offset_1 : offsetSaved;
  139. rep[1] = offset_2 ? offset_2 : offsetSaved;
  140. /* Return the last literals size */
  141. return iend - anchor;
  142. }
  143. size_t ZSTD_compressBlock_doubleFast(
  144. ZSTD_matchState_t* ms, seqStore_t* seqStore, U32 rep[ZSTD_REP_NUM],
  145. ZSTD_compressionParameters const* cParams, void const* src, size_t srcSize)
  146. {
  147. const U32 mls = cParams->searchLength;
  148. switch(mls)
  149. {
  150. default: /* includes case 3 */
  151. case 4 :
  152. return ZSTD_compressBlock_doubleFast_generic(ms, seqStore, rep, cParams, src, srcSize, 4);
  153. case 5 :
  154. return ZSTD_compressBlock_doubleFast_generic(ms, seqStore, rep, cParams, src, srcSize, 5);
  155. case 6 :
  156. return ZSTD_compressBlock_doubleFast_generic(ms, seqStore, rep, cParams, src, srcSize, 6);
  157. case 7 :
  158. return ZSTD_compressBlock_doubleFast_generic(ms, seqStore, rep, cParams, src, srcSize, 7);
  159. }
  160. }
  161. static size_t ZSTD_compressBlock_doubleFast_extDict_generic(
  162. ZSTD_matchState_t* ms, seqStore_t* seqStore, U32 rep[ZSTD_REP_NUM],
  163. ZSTD_compressionParameters const* cParams, void const* src, size_t srcSize,
  164. U32 const mls /* template */)
  165. {
  166. U32* const hashLong = ms->hashTable;
  167. U32 const hBitsL = cParams->hashLog;
  168. U32* const hashSmall = ms->chainTable;
  169. U32 const hBitsS = cParams->chainLog;
  170. const BYTE* const base = ms->window.base;
  171. const BYTE* const dictBase = ms->window.dictBase;
  172. const BYTE* const istart = (const BYTE*)src;
  173. const BYTE* ip = istart;
  174. const BYTE* anchor = istart;
  175. const U32 lowestIndex = ms->window.lowLimit;
  176. const BYTE* const dictStart = dictBase + lowestIndex;
  177. const U32 dictLimit = ms->window.dictLimit;
  178. const BYTE* const lowPrefixPtr = base + dictLimit;
  179. const BYTE* const dictEnd = dictBase + dictLimit;
  180. const BYTE* const iend = istart + srcSize;
  181. const BYTE* const ilimit = iend - 8;
  182. U32 offset_1=rep[0], offset_2=rep[1];
  183. /* Search Loop */
  184. while (ip < ilimit) { /* < instead of <=, because (ip+1) */
  185. const size_t hSmall = ZSTD_hashPtr(ip, hBitsS, mls);
  186. const U32 matchIndex = hashSmall[hSmall];
  187. const BYTE* matchBase = matchIndex < dictLimit ? dictBase : base;
  188. const BYTE* match = matchBase + matchIndex;
  189. const size_t hLong = ZSTD_hashPtr(ip, hBitsL, 8);
  190. const U32 matchLongIndex = hashLong[hLong];
  191. const BYTE* matchLongBase = matchLongIndex < dictLimit ? dictBase : base;
  192. const BYTE* matchLong = matchLongBase + matchLongIndex;
  193. const U32 current = (U32)(ip-base);
  194. const U32 repIndex = current + 1 - offset_1; /* offset_1 expected <= current +1 */
  195. const BYTE* repBase = repIndex < dictLimit ? dictBase : base;
  196. const BYTE* repMatch = repBase + repIndex;
  197. size_t mLength;
  198. hashSmall[hSmall] = hashLong[hLong] = current; /* update hash table */
  199. if ( (((U32)((dictLimit-1) - repIndex) >= 3) /* intentional underflow */ & (repIndex > lowestIndex))
  200. && (MEM_read32(repMatch) == MEM_read32(ip+1)) ) {
  201. const BYTE* repMatchEnd = repIndex < dictLimit ? dictEnd : iend;
  202. mLength = ZSTD_count_2segments(ip+1+4, repMatch+4, iend, repMatchEnd, lowPrefixPtr) + 4;
  203. ip++;
  204. ZSTD_storeSeq(seqStore, ip-anchor, anchor, 0, mLength-MINMATCH);
  205. } else {
  206. if ((matchLongIndex > lowestIndex) && (MEM_read64(matchLong) == MEM_read64(ip))) {
  207. const BYTE* matchEnd = matchLongIndex < dictLimit ? dictEnd : iend;
  208. const BYTE* lowMatchPtr = matchLongIndex < dictLimit ? dictStart : lowPrefixPtr;
  209. U32 offset;
  210. mLength = ZSTD_count_2segments(ip+8, matchLong+8, iend, matchEnd, lowPrefixPtr) + 8;
  211. offset = current - matchLongIndex;
  212. while (((ip>anchor) & (matchLong>lowMatchPtr)) && (ip[-1] == matchLong[-1])) { ip--; matchLong--; mLength++; } /* catch up */
  213. offset_2 = offset_1;
  214. offset_1 = offset;
  215. ZSTD_storeSeq(seqStore, ip-anchor, anchor, offset + ZSTD_REP_MOVE, mLength-MINMATCH);
  216. } else if ((matchIndex > lowestIndex) && (MEM_read32(match) == MEM_read32(ip))) {
  217. size_t const h3 = ZSTD_hashPtr(ip+1, hBitsL, 8);
  218. U32 const matchIndex3 = hashLong[h3];
  219. const BYTE* const match3Base = matchIndex3 < dictLimit ? dictBase : base;
  220. const BYTE* match3 = match3Base + matchIndex3;
  221. U32 offset;
  222. hashLong[h3] = current + 1;
  223. if ( (matchIndex3 > lowestIndex) && (MEM_read64(match3) == MEM_read64(ip+1)) ) {
  224. const BYTE* matchEnd = matchIndex3 < dictLimit ? dictEnd : iend;
  225. const BYTE* lowMatchPtr = matchIndex3 < dictLimit ? dictStart : lowPrefixPtr;
  226. mLength = ZSTD_count_2segments(ip+9, match3+8, iend, matchEnd, lowPrefixPtr) + 8;
  227. ip++;
  228. offset = current+1 - matchIndex3;
  229. while (((ip>anchor) & (match3>lowMatchPtr)) && (ip[-1] == match3[-1])) { ip--; match3--; mLength++; } /* catch up */
  230. } else {
  231. const BYTE* matchEnd = matchIndex < dictLimit ? dictEnd : iend;
  232. const BYTE* lowMatchPtr = matchIndex < dictLimit ? dictStart : lowPrefixPtr;
  233. mLength = ZSTD_count_2segments(ip+4, match+4, iend, matchEnd, lowPrefixPtr) + 4;
  234. offset = current - matchIndex;
  235. while (((ip>anchor) & (match>lowMatchPtr)) && (ip[-1] == match[-1])) { ip--; match--; mLength++; } /* catch up */
  236. }
  237. offset_2 = offset_1;
  238. offset_1 = offset;
  239. ZSTD_storeSeq(seqStore, ip-anchor, anchor, offset + ZSTD_REP_MOVE, mLength-MINMATCH);
  240. } else {
  241. ip += ((ip-anchor) >> kSearchStrength) + 1;
  242. continue;
  243. } }
  244. /* found a match : store it */
  245. ip += mLength;
  246. anchor = ip;
  247. if (ip <= ilimit) {
  248. /* Fill Table */
  249. hashSmall[ZSTD_hashPtr(base+current+2, hBitsS, mls)] = current+2;
  250. hashLong[ZSTD_hashPtr(base+current+2, hBitsL, 8)] = current+2;
  251. hashSmall[ZSTD_hashPtr(ip-2, hBitsS, mls)] = (U32)(ip-2-base);
  252. hashLong[ZSTD_hashPtr(ip-2, hBitsL, 8)] = (U32)(ip-2-base);
  253. /* check immediate repcode */
  254. while (ip <= ilimit) {
  255. U32 const current2 = (U32)(ip-base);
  256. U32 const repIndex2 = current2 - offset_2;
  257. const BYTE* repMatch2 = repIndex2 < dictLimit ? dictBase + repIndex2 : base + repIndex2;
  258. if ( (((U32)((dictLimit-1) - repIndex2) >= 3) & (repIndex2 > lowestIndex)) /* intentional overflow */
  259. && (MEM_read32(repMatch2) == MEM_read32(ip)) ) {
  260. const BYTE* const repEnd2 = repIndex2 < dictLimit ? dictEnd : iend;
  261. size_t const repLength2 = ZSTD_count_2segments(ip+4, repMatch2+4, iend, repEnd2, lowPrefixPtr) + 4;
  262. U32 tmpOffset = offset_2; offset_2 = offset_1; offset_1 = tmpOffset; /* swap offset_2 <=> offset_1 */
  263. ZSTD_storeSeq(seqStore, 0, anchor, 0, repLength2-MINMATCH);
  264. hashSmall[ZSTD_hashPtr(ip, hBitsS, mls)] = current2;
  265. hashLong[ZSTD_hashPtr(ip, hBitsL, 8)] = current2;
  266. ip += repLength2;
  267. anchor = ip;
  268. continue;
  269. }
  270. break;
  271. } } }
  272. /* save reps for next block */
  273. rep[0] = offset_1;
  274. rep[1] = offset_2;
  275. /* Return the last literals size */
  276. return iend - anchor;
  277. }
  278. size_t ZSTD_compressBlock_doubleFast_extDict(
  279. ZSTD_matchState_t* ms, seqStore_t* seqStore, U32 rep[ZSTD_REP_NUM],
  280. ZSTD_compressionParameters const* cParams, void const* src, size_t srcSize)
  281. {
  282. U32 const mls = cParams->searchLength;
  283. switch(mls)
  284. {
  285. default: /* includes case 3 */
  286. case 4 :
  287. return ZSTD_compressBlock_doubleFast_extDict_generic(ms, seqStore, rep, cParams, src, srcSize, 4);
  288. case 5 :
  289. return ZSTD_compressBlock_doubleFast_extDict_generic(ms, seqStore, rep, cParams, src, srcSize, 5);
  290. case 6 :
  291. return ZSTD_compressBlock_doubleFast_extDict_generic(ms, seqStore, rep, cParams, src, srcSize, 6);
  292. case 7 :
  293. return ZSTD_compressBlock_doubleFast_extDict_generic(ms, seqStore, rep, cParams, src, srcSize, 7);
  294. }
  295. }