zstd_fast.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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_fast.h"
  12. void ZSTD_fillHashTable(ZSTD_matchState_t* ms,
  13. ZSTD_compressionParameters const* cParams,
  14. void const* end)
  15. {
  16. U32* const hashTable = ms->hashTable;
  17. U32 const hBits = cParams->hashLog;
  18. U32 const mls = cParams->searchLength;
  19. const BYTE* const base = ms->window.base;
  20. const BYTE* ip = base + ms->nextToUpdate;
  21. const BYTE* const iend = ((const BYTE*)end) - HASH_READ_SIZE;
  22. const U32 fastHashFillStep = 3;
  23. /* Always insert every fastHashFillStep position into the hash table.
  24. * Insert the other positions if their hash entry is empty.
  25. */
  26. for (; ip + fastHashFillStep - 1 <= iend; ip += fastHashFillStep) {
  27. U32 const current = (U32)(ip - base);
  28. U32 i;
  29. for (i = 0; i < fastHashFillStep; ++i) {
  30. size_t const hash = ZSTD_hashPtr(ip + i, hBits, mls);
  31. if (i == 0 || hashTable[hash] == 0)
  32. hashTable[hash] = current + i;
  33. }
  34. }
  35. }
  36. FORCE_INLINE_TEMPLATE
  37. size_t ZSTD_compressBlock_fast_generic(
  38. ZSTD_matchState_t* ms, seqStore_t* seqStore, U32 rep[ZSTD_REP_NUM],
  39. void const* src, size_t srcSize,
  40. U32 const hlog, U32 const stepSize, U32 const mls)
  41. {
  42. U32* const hashTable = ms->hashTable;
  43. const BYTE* const base = ms->window.base;
  44. const BYTE* const istart = (const BYTE*)src;
  45. const BYTE* ip = istart;
  46. const BYTE* anchor = istart;
  47. const U32 lowestIndex = ms->window.dictLimit;
  48. const BYTE* const lowest = base + lowestIndex;
  49. const BYTE* const iend = istart + srcSize;
  50. const BYTE* const ilimit = iend - HASH_READ_SIZE;
  51. U32 offset_1=rep[0], offset_2=rep[1];
  52. U32 offsetSaved = 0;
  53. /* init */
  54. ip += (ip==lowest);
  55. { U32 const maxRep = (U32)(ip-lowest);
  56. if (offset_2 > maxRep) offsetSaved = offset_2, offset_2 = 0;
  57. if (offset_1 > maxRep) offsetSaved = offset_1, offset_1 = 0;
  58. }
  59. /* Main Search Loop */
  60. while (ip < ilimit) { /* < instead of <=, because repcode check at (ip+1) */
  61. size_t mLength;
  62. size_t const h = ZSTD_hashPtr(ip, hlog, mls);
  63. U32 const current = (U32)(ip-base);
  64. U32 const matchIndex = hashTable[h];
  65. const BYTE* match = base + matchIndex;
  66. hashTable[h] = current; /* update hash table */
  67. if ((offset_1 > 0) & (MEM_read32(ip+1-offset_1) == MEM_read32(ip+1))) {
  68. mLength = ZSTD_count(ip+1+4, ip+1+4-offset_1, iend) + 4;
  69. ip++;
  70. ZSTD_storeSeq(seqStore, ip-anchor, anchor, 0, mLength-MINMATCH);
  71. } else {
  72. if ( (matchIndex <= lowestIndex)
  73. || (MEM_read32(match) != MEM_read32(ip)) ) {
  74. assert(stepSize >= 1);
  75. ip += ((ip-anchor) >> kSearchStrength) + stepSize;
  76. continue;
  77. }
  78. mLength = ZSTD_count(ip+4, match+4, iend) + 4;
  79. { U32 const offset = (U32)(ip-match);
  80. while (((ip>anchor) & (match>lowest)) && (ip[-1] == match[-1])) { ip--; match--; mLength++; } /* catch up */
  81. offset_2 = offset_1;
  82. offset_1 = offset;
  83. ZSTD_storeSeq(seqStore, ip-anchor, anchor, offset + ZSTD_REP_MOVE, mLength-MINMATCH);
  84. } }
  85. /* match found */
  86. ip += mLength;
  87. anchor = ip;
  88. if (ip <= ilimit) {
  89. /* Fill Table */
  90. hashTable[ZSTD_hashPtr(base+current+2, hlog, mls)] = current+2; /* here because current+2 could be > iend-8 */
  91. hashTable[ZSTD_hashPtr(ip-2, hlog, mls)] = (U32)(ip-2-base);
  92. /* check immediate repcode */
  93. while ( (ip <= ilimit)
  94. && ( (offset_2>0)
  95. & (MEM_read32(ip) == MEM_read32(ip - offset_2)) )) {
  96. /* store sequence */
  97. size_t const rLength = ZSTD_count(ip+4, ip+4-offset_2, iend) + 4;
  98. { U32 const tmpOff = offset_2; offset_2 = offset_1; offset_1 = tmpOff; } /* swap offset_2 <=> offset_1 */
  99. hashTable[ZSTD_hashPtr(ip, hlog, mls)] = (U32)(ip-base);
  100. ZSTD_storeSeq(seqStore, 0, anchor, 0, rLength-MINMATCH);
  101. ip += rLength;
  102. anchor = ip;
  103. continue; /* faster when present ... (?) */
  104. } } }
  105. /* save reps for next block */
  106. rep[0] = offset_1 ? offset_1 : offsetSaved;
  107. rep[1] = offset_2 ? offset_2 : offsetSaved;
  108. /* Return the last literals size */
  109. return iend - anchor;
  110. }
  111. size_t ZSTD_compressBlock_fast(
  112. ZSTD_matchState_t* ms, seqStore_t* seqStore, U32 rep[ZSTD_REP_NUM],
  113. ZSTD_compressionParameters const* cParams, void const* src, size_t srcSize)
  114. {
  115. U32 const hlog = cParams->hashLog;
  116. U32 const mls = cParams->searchLength;
  117. U32 const stepSize = cParams->targetLength;
  118. switch(mls)
  119. {
  120. default: /* includes case 3 */
  121. case 4 :
  122. return ZSTD_compressBlock_fast_generic(ms, seqStore, rep, src, srcSize, hlog, stepSize, 4);
  123. case 5 :
  124. return ZSTD_compressBlock_fast_generic(ms, seqStore, rep, src, srcSize, hlog, stepSize, 5);
  125. case 6 :
  126. return ZSTD_compressBlock_fast_generic(ms, seqStore, rep, src, srcSize, hlog, stepSize, 6);
  127. case 7 :
  128. return ZSTD_compressBlock_fast_generic(ms, seqStore, rep, src, srcSize, hlog, stepSize, 7);
  129. }
  130. }
  131. static size_t ZSTD_compressBlock_fast_extDict_generic(
  132. ZSTD_matchState_t* ms, seqStore_t* seqStore, U32 rep[ZSTD_REP_NUM],
  133. void const* src, size_t srcSize,
  134. U32 const hlog, U32 const stepSize, U32 const mls)
  135. {
  136. U32* hashTable = ms->hashTable;
  137. const BYTE* const base = ms->window.base;
  138. const BYTE* const dictBase = ms->window.dictBase;
  139. const BYTE* const istart = (const BYTE*)src;
  140. const BYTE* ip = istart;
  141. const BYTE* anchor = istart;
  142. const U32 lowestIndex = ms->window.lowLimit;
  143. const BYTE* const dictStart = dictBase + lowestIndex;
  144. const U32 dictLimit = ms->window.dictLimit;
  145. const BYTE* const lowPrefixPtr = base + dictLimit;
  146. const BYTE* const dictEnd = dictBase + dictLimit;
  147. const BYTE* const iend = istart + srcSize;
  148. const BYTE* const ilimit = iend - 8;
  149. U32 offset_1=rep[0], offset_2=rep[1];
  150. /* Search Loop */
  151. while (ip < ilimit) { /* < instead of <=, because (ip+1) */
  152. const size_t h = ZSTD_hashPtr(ip, hlog, mls);
  153. const U32 matchIndex = hashTable[h];
  154. const BYTE* matchBase = matchIndex < dictLimit ? dictBase : base;
  155. const BYTE* match = matchBase + matchIndex;
  156. const U32 current = (U32)(ip-base);
  157. const U32 repIndex = current + 1 - offset_1; /* offset_1 expected <= current +1 */
  158. const BYTE* repBase = repIndex < dictLimit ? dictBase : base;
  159. const BYTE* repMatch = repBase + repIndex;
  160. size_t mLength;
  161. hashTable[h] = current; /* update hash table */
  162. if ( (((U32)((dictLimit-1) - repIndex) >= 3) /* intentional underflow */ & (repIndex > lowestIndex))
  163. && (MEM_read32(repMatch) == MEM_read32(ip+1)) ) {
  164. const BYTE* repMatchEnd = repIndex < dictLimit ? dictEnd : iend;
  165. mLength = ZSTD_count_2segments(ip+1+4, repMatch+4, iend, repMatchEnd, lowPrefixPtr) + 4;
  166. ip++;
  167. ZSTD_storeSeq(seqStore, ip-anchor, anchor, 0, mLength-MINMATCH);
  168. } else {
  169. if ( (matchIndex < lowestIndex) ||
  170. (MEM_read32(match) != MEM_read32(ip)) ) {
  171. assert(stepSize >= 1);
  172. ip += ((ip-anchor) >> kSearchStrength) + stepSize;
  173. continue;
  174. }
  175. { const BYTE* matchEnd = matchIndex < dictLimit ? dictEnd : iend;
  176. const BYTE* lowMatchPtr = matchIndex < dictLimit ? dictStart : lowPrefixPtr;
  177. U32 offset;
  178. mLength = ZSTD_count_2segments(ip+4, match+4, iend, matchEnd, lowPrefixPtr) + 4;
  179. while (((ip>anchor) & (match>lowMatchPtr)) && (ip[-1] == match[-1])) { ip--; match--; mLength++; } /* catch up */
  180. offset = current - matchIndex;
  181. offset_2 = offset_1;
  182. offset_1 = offset;
  183. ZSTD_storeSeq(seqStore, ip-anchor, anchor, offset + ZSTD_REP_MOVE, mLength-MINMATCH);
  184. } }
  185. /* found a match : store it */
  186. ip += mLength;
  187. anchor = ip;
  188. if (ip <= ilimit) {
  189. /* Fill Table */
  190. hashTable[ZSTD_hashPtr(base+current+2, hlog, mls)] = current+2;
  191. hashTable[ZSTD_hashPtr(ip-2, hlog, mls)] = (U32)(ip-2-base);
  192. /* check immediate repcode */
  193. while (ip <= ilimit) {
  194. U32 const current2 = (U32)(ip-base);
  195. U32 const repIndex2 = current2 - offset_2;
  196. const BYTE* repMatch2 = repIndex2 < dictLimit ? dictBase + repIndex2 : base + repIndex2;
  197. if ( (((U32)((dictLimit-1) - repIndex2) >= 3) & (repIndex2 > lowestIndex)) /* intentional overflow */
  198. && (MEM_read32(repMatch2) == MEM_read32(ip)) ) {
  199. const BYTE* const repEnd2 = repIndex2 < dictLimit ? dictEnd : iend;
  200. size_t const repLength2 = ZSTD_count_2segments(ip+4, repMatch2+4, iend, repEnd2, lowPrefixPtr) + 4;
  201. U32 tmpOffset = offset_2; offset_2 = offset_1; offset_1 = tmpOffset; /* swap offset_2 <=> offset_1 */
  202. ZSTD_storeSeq(seqStore, 0, anchor, 0, repLength2-MINMATCH);
  203. hashTable[ZSTD_hashPtr(ip, hlog, mls)] = current2;
  204. ip += repLength2;
  205. anchor = ip;
  206. continue;
  207. }
  208. break;
  209. } } }
  210. /* save reps for next block */
  211. rep[0] = offset_1;
  212. rep[1] = offset_2;
  213. /* Return the last literals size */
  214. return iend - anchor;
  215. }
  216. size_t ZSTD_compressBlock_fast_extDict(
  217. ZSTD_matchState_t* ms, seqStore_t* seqStore, U32 rep[ZSTD_REP_NUM],
  218. ZSTD_compressionParameters const* cParams, void const* src, size_t srcSize)
  219. {
  220. U32 const hlog = cParams->hashLog;
  221. U32 const mls = cParams->searchLength;
  222. U32 const stepSize = cParams->targetLength;
  223. switch(mls)
  224. {
  225. default: /* includes case 3 */
  226. case 4 :
  227. return ZSTD_compressBlock_fast_extDict_generic(ms, seqStore, rep, src, srcSize, hlog, stepSize, 4);
  228. case 5 :
  229. return ZSTD_compressBlock_fast_extDict_generic(ms, seqStore, rep, src, srcSize, hlog, stepSize, 5);
  230. case 6 :
  231. return ZSTD_compressBlock_fast_extDict_generic(ms, seqStore, rep, src, srcSize, hlog, stepSize, 6);
  232. case 7 :
  233. return ZSTD_compressBlock_fast_extDict_generic(ms, seqStore, rep, src, srcSize, hlog, stepSize, 7);
  234. }
  235. }