zstd_double_fast.c 14 KB

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