hash_longest_match64_inc.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. /* NOLINT(build/header_guard) */
  2. /* Copyright 2010 Google Inc. All Rights Reserved.
  3. Distributed under MIT license.
  4. See file LICENSE for detail or copy at https://opensource.org/licenses/MIT
  5. */
  6. /* template parameters: FN */
  7. /* A (forgetful) hash table to the data seen by the compressor, to
  8. help create backward references to previous data.
  9. This is a hash map of fixed size (bucket_size_) to a ring buffer of
  10. fixed size (block_size_). The ring buffer contains the last block_size_
  11. index positions of the given hash key in the compressed data. */
  12. #define HashLongestMatch HASHER()
  13. static BROTLI_INLINE size_t FN(HashTypeLength)(void) { return 8; }
  14. static BROTLI_INLINE size_t FN(StoreLookahead)(void) { return 8; }
  15. /* HashBytes is the function that chooses the bucket to place the address in. */
  16. static BROTLI_INLINE uint32_t FN(HashBytes)(const uint8_t* data,
  17. const uint64_t mask,
  18. const int shift) {
  19. const uint64_t h = (BROTLI_UNALIGNED_LOAD64LE(data) & mask) * kHashMul64Long;
  20. /* The higher bits contain more mixture from the multiplication,
  21. so we take our results from there. */
  22. return (uint32_t)(h >> shift);
  23. }
  24. typedef struct HashLongestMatch {
  25. /* Number of hash buckets. */
  26. size_t bucket_size_;
  27. /* Only block_size_ newest backward references are kept,
  28. and the older are forgotten. */
  29. size_t block_size_;
  30. /* Left-shift for computing hash bucket index from hash value. */
  31. int hash_shift_;
  32. /* Mask for selecting the next 4-8 bytes of input */
  33. uint64_t hash_mask_;
  34. /* Mask for accessing entries in a block (in a ring-buffer manner). */
  35. uint32_t block_mask_;
  36. /* --- Dynamic size members --- */
  37. /* Number of entries in a particular bucket. */
  38. /* uint16_t num[bucket_size]; */
  39. /* Buckets containing block_size_ of backward references. */
  40. /* uint32_t* buckets[bucket_size * block_size]; */
  41. } HashLongestMatch;
  42. static BROTLI_INLINE HashLongestMatch* FN(Self)(HasherHandle handle) {
  43. return (HashLongestMatch*)&(GetHasherCommon(handle)[1]);
  44. }
  45. static BROTLI_INLINE uint16_t* FN(Num)(HashLongestMatch* self) {
  46. return (uint16_t*)(&self[1]);
  47. }
  48. static BROTLI_INLINE uint32_t* FN(Buckets)(HashLongestMatch* self) {
  49. return (uint32_t*)(&FN(Num)(self)[self->bucket_size_]);
  50. }
  51. static void FN(Initialize)(
  52. HasherHandle handle, const BrotliEncoderParams* params) {
  53. HasherCommon* common = GetHasherCommon(handle);
  54. HashLongestMatch* self = FN(Self)(handle);
  55. BROTLI_UNUSED(params);
  56. self->hash_shift_ = 64 - common->params.bucket_bits;
  57. self->hash_mask_ = (~((uint64_t)0U)) >> (64 - 8 * common->params.hash_len);
  58. self->bucket_size_ = (size_t)1 << common->params.bucket_bits;
  59. self->block_size_ = (size_t)1 << common->params.block_bits;
  60. self->block_mask_ = (uint32_t)(self->block_size_ - 1);
  61. }
  62. static void FN(Prepare)(HasherHandle handle, BROTLI_BOOL one_shot,
  63. size_t input_size, const uint8_t* data) {
  64. HashLongestMatch* self = FN(Self)(handle);
  65. uint16_t* num = FN(Num)(self);
  66. /* Partial preparation is 100 times slower (per socket). */
  67. size_t partial_prepare_threshold = self->bucket_size_ >> 6;
  68. if (one_shot && input_size <= partial_prepare_threshold) {
  69. size_t i;
  70. for (i = 0; i < input_size; ++i) {
  71. const uint32_t key = FN(HashBytes)(&data[i], self->hash_mask_,
  72. self->hash_shift_);
  73. num[key] = 0;
  74. }
  75. } else {
  76. memset(num, 0, self->bucket_size_ * sizeof(num[0]));
  77. }
  78. }
  79. static BROTLI_INLINE size_t FN(HashMemAllocInBytes)(
  80. const BrotliEncoderParams* params, BROTLI_BOOL one_shot,
  81. size_t input_size) {
  82. size_t bucket_size = (size_t)1 << params->hasher.bucket_bits;
  83. size_t block_size = (size_t)1 << params->hasher.block_bits;
  84. BROTLI_UNUSED(one_shot);
  85. BROTLI_UNUSED(input_size);
  86. return sizeof(HashLongestMatch) + bucket_size * (2 + 4 * block_size);
  87. }
  88. /* Look at 4 bytes at &data[ix & mask].
  89. Compute a hash from these, and store the value of ix at that position. */
  90. static BROTLI_INLINE void FN(Store)(HasherHandle handle, const uint8_t* data,
  91. const size_t mask, const size_t ix) {
  92. HashLongestMatch* self = FN(Self)(handle);
  93. uint16_t* num = FN(Num)(self);
  94. const uint32_t key = FN(HashBytes)(&data[ix & mask], self->hash_mask_,
  95. self->hash_shift_);
  96. const size_t minor_ix = num[key] & self->block_mask_;
  97. const size_t offset =
  98. minor_ix + (key << GetHasherCommon(handle)->params.block_bits);
  99. FN(Buckets)(self)[offset] = (uint32_t)ix;
  100. ++num[key];
  101. }
  102. static BROTLI_INLINE void FN(StoreRange)(HasherHandle handle,
  103. const uint8_t* data, const size_t mask, const size_t ix_start,
  104. const size_t ix_end) {
  105. size_t i;
  106. for (i = ix_start; i < ix_end; ++i) {
  107. FN(Store)(handle, data, mask, i);
  108. }
  109. }
  110. static BROTLI_INLINE void FN(StitchToPreviousBlock)(HasherHandle handle,
  111. size_t num_bytes, size_t position, const uint8_t* ringbuffer,
  112. size_t ringbuffer_mask) {
  113. if (num_bytes >= FN(HashTypeLength)() - 1 && position >= 3) {
  114. /* Prepare the hashes for three last bytes of the last write.
  115. These could not be calculated before, since they require knowledge
  116. of both the previous and the current block. */
  117. FN(Store)(handle, ringbuffer, ringbuffer_mask, position - 3);
  118. FN(Store)(handle, ringbuffer, ringbuffer_mask, position - 2);
  119. FN(Store)(handle, ringbuffer, ringbuffer_mask, position - 1);
  120. }
  121. }
  122. static BROTLI_INLINE void FN(PrepareDistanceCache)(
  123. HasherHandle handle, int* BROTLI_RESTRICT distance_cache) {
  124. PrepareDistanceCache(distance_cache,
  125. GetHasherCommon(handle)->params.num_last_distances_to_check);
  126. }
  127. /* Find a longest backward match of &data[cur_ix] up to the length of
  128. max_length and stores the position cur_ix in the hash table.
  129. REQUIRES: FN(PrepareDistanceCache) must be invoked for current distance cache
  130. values; if this method is invoked repeatedly with the same distance
  131. cache values, it is enough to invoke FN(PrepareDistanceCache) once.
  132. Does not look for matches longer than max_length.
  133. Does not look for matches further away than max_backward.
  134. Writes the best match into |out|.
  135. |out|->score is updated only if a better match is found. */
  136. static BROTLI_INLINE void FN(FindLongestMatch)(HasherHandle handle,
  137. const BrotliEncoderDictionary* dictionary,
  138. const uint8_t* BROTLI_RESTRICT data, const size_t ring_buffer_mask,
  139. const int* BROTLI_RESTRICT distance_cache, const size_t cur_ix,
  140. const size_t max_length, const size_t max_backward,
  141. const size_t gap, const size_t max_distance,
  142. HasherSearchResult* BROTLI_RESTRICT out) {
  143. HasherCommon* common = GetHasherCommon(handle);
  144. HashLongestMatch* self = FN(Self)(handle);
  145. uint16_t* num = FN(Num)(self);
  146. uint32_t* buckets = FN(Buckets)(self);
  147. const size_t cur_ix_masked = cur_ix & ring_buffer_mask;
  148. /* Don't accept a short copy from far away. */
  149. score_t min_score = out->score;
  150. score_t best_score = out->score;
  151. size_t best_len = out->len;
  152. size_t i;
  153. out->len = 0;
  154. out->len_code_delta = 0;
  155. /* Try last distance first. */
  156. for (i = 0; i < (size_t)common->params.num_last_distances_to_check; ++i) {
  157. const size_t backward = (size_t)distance_cache[i];
  158. size_t prev_ix = (size_t)(cur_ix - backward);
  159. if (prev_ix >= cur_ix) {
  160. continue;
  161. }
  162. if (BROTLI_PREDICT_FALSE(backward > max_backward)) {
  163. continue;
  164. }
  165. prev_ix &= ring_buffer_mask;
  166. if (cur_ix_masked + best_len > ring_buffer_mask ||
  167. prev_ix + best_len > ring_buffer_mask ||
  168. data[cur_ix_masked + best_len] != data[prev_ix + best_len]) {
  169. continue;
  170. }
  171. {
  172. const size_t len = FindMatchLengthWithLimit(&data[prev_ix],
  173. &data[cur_ix_masked],
  174. max_length);
  175. if (len >= 3 || (len == 2 && i < 2)) {
  176. /* Comparing for >= 2 does not change the semantics, but just saves for
  177. a few unnecessary binary logarithms in backward reference score,
  178. since we are not interested in such short matches. */
  179. score_t score = BackwardReferenceScoreUsingLastDistance(len);
  180. if (best_score < score) {
  181. if (i != 0) score -= BackwardReferencePenaltyUsingLastDistance(i);
  182. if (best_score < score) {
  183. best_score = score;
  184. best_len = len;
  185. out->len = best_len;
  186. out->distance = backward;
  187. out->score = best_score;
  188. }
  189. }
  190. }
  191. }
  192. }
  193. {
  194. const uint32_t key = FN(HashBytes)(
  195. &data[cur_ix_masked], self->hash_mask_, self->hash_shift_);
  196. uint32_t* BROTLI_RESTRICT bucket =
  197. &buckets[key << common->params.block_bits];
  198. const size_t down =
  199. (num[key] > self->block_size_) ?
  200. (num[key] - self->block_size_) : 0u;
  201. for (i = num[key]; i > down;) {
  202. size_t prev_ix = bucket[--i & self->block_mask_];
  203. const size_t backward = cur_ix - prev_ix;
  204. if (BROTLI_PREDICT_FALSE(backward > max_backward)) {
  205. break;
  206. }
  207. prev_ix &= ring_buffer_mask;
  208. if (cur_ix_masked + best_len > ring_buffer_mask ||
  209. prev_ix + best_len > ring_buffer_mask ||
  210. data[cur_ix_masked + best_len] != data[prev_ix + best_len]) {
  211. continue;
  212. }
  213. {
  214. const size_t len = FindMatchLengthWithLimit(&data[prev_ix],
  215. &data[cur_ix_masked],
  216. max_length);
  217. if (len >= 4) {
  218. /* Comparing for >= 3 does not change the semantics, but just saves
  219. for a few unnecessary binary logarithms in backward reference
  220. score, since we are not interested in such short matches. */
  221. score_t score = BackwardReferenceScore(len, backward);
  222. if (best_score < score) {
  223. best_score = score;
  224. best_len = len;
  225. out->len = best_len;
  226. out->distance = backward;
  227. out->score = best_score;
  228. }
  229. }
  230. }
  231. }
  232. bucket[num[key] & self->block_mask_] = (uint32_t)cur_ix;
  233. ++num[key];
  234. }
  235. if (min_score == out->score) {
  236. SearchInStaticDictionary(dictionary,
  237. handle, &data[cur_ix_masked], max_length, max_backward + gap,
  238. max_distance, out, BROTLI_FALSE);
  239. }
  240. }
  241. #undef HashLongestMatch