You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

zstd_fast.c 10.0KB

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