| 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 | #define TAG_HASH_BITS 8 |
| 14 | #define TAG_HASH_MASK ((1 << TAG_HASH_BITS) - 1) |
| 15 | static BROTLI_INLINE size_t FN(HashTypeLength)(void) { return 4; } |
| 16 | static BROTLI_INLINE size_t FN(StoreLookahead)(void) { return 4; } |
| 17 | /* HashBytes is the function that chooses the bucket to place the address in. */ |
| 18 | static uint32_t FN(HashBytes)( |
| 19 | const uint8_t* BROTLI_RESTRICT data, const int shift) { |
| 20 | uint32_t h = BROTLI_UNALIGNED_LOAD32LE(data) * kHashMul32; |
| 21 | /* The higher bits contain more mixture from the multiplication, |
| 22 | so we take our results from there. */ |
| 23 | return (uint32_t)(h >> shift); |
| 24 | } |
| 25 | typedef struct HashLongestMatch { |
| 26 | /* Number of hash buckets. */ |
| 27 | size_t bucket_size_; |
| 28 | /* Only block_size_ newest backward references are kept, |
| 29 | and the older are forgotten. */ |
| 30 | size_t block_size_; |
| 31 | /* Left-shift for computing hash bucket index from hash value. */ |
| 32 | int hash_shift_; |
| 33 | /* Mask for accessing entries in a block (in a ring-buffer manner). */ |
| 34 | uint32_t block_mask_; |
| 35 | int block_bits_; |
| 36 | int num_last_distances_to_check_; |
| 37 | /* Shortcuts. */ |
| 38 | HasherCommon* common_; |
| 39 | /* --- Dynamic size members --- */ |
| 40 | /* Number of entries in a particular bucket. */ |
| 41 | uint16_t* num_; /* uint16_t[bucket_size]; */ |
| 42 | uint8_t* tags_; |
| 43 | /* Buckets containing block_size_ of backward references. */ |
| 44 | uint32_t* buckets_; /* uint32_t[bucket_size * block_size]; */ |
| 45 | } HashLongestMatch; |
| 46 | static void FN(Initialize)( |
| 47 | HasherCommon* common, HashLongestMatch* BROTLI_RESTRICT self, |
| 48 | const BrotliEncoderParams* params) { |
| 49 | self->common_ = common; |
| 50 | BROTLI_UNUSED(params); |
| 51 | self->hash_shift_ = 32 - common->params.bucket_bits - TAG_HASH_BITS; |
| 52 | self->bucket_size_ = (size_t)1 << common->params.bucket_bits; |
| 53 | self->block_size_ = (size_t)1 << common->params.block_bits; |
| 54 | self->block_mask_ = (uint32_t)(self->block_size_ - 1); |
| 55 | self->num_ = (uint16_t*)common->extra[0]; |
| 56 | self->tags_ = (uint8_t*)common->extra[1]; |
| 57 | self->buckets_ = (uint32_t*)common->extra[2]; |
| 58 | self->block_bits_ = common->params.block_bits; |
| 59 | self->num_last_distances_to_check_ = |
| 60 | common->params.num_last_distances_to_check; |
| 61 | } |
| 62 | static void FN(Prepare)( |
| 63 | HashLongestMatch* BROTLI_RESTRICT self, BROTLI_BOOL one_shot, |
| 64 | size_t input_size, const uint8_t* BROTLI_RESTRICT data) { |
| 65 | uint16_t* BROTLI_RESTRICT num = self->num_; |
| 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 hash = FN(HashBytes)(&data[i], self->hash_shift_); |
| 72 | const uint32_t key = hash >> TAG_HASH_BITS; |
| 73 | num[key] = 65535; |
| 74 | } |
| 75 | } else { |
| 76 | /* Set all the bytes of num to 255, which makes each uint16_t 65535. */ |
| 77 | memset(num, 255, self->bucket_size_ * sizeof(num[0])); |
| 78 | } |
| 79 | } |
| 80 | static BROTLI_INLINE void FN(HashMemAllocInBytes)( |
| 81 | const BrotliEncoderParams* params, BROTLI_BOOL one_shot, |
| 82 | size_t input_size, size_t* alloc_size) { |
| 83 | size_t bucket_size = (size_t)1 << params->hasher.bucket_bits; |
| 84 | size_t block_size = (size_t)1 << params->hasher.block_bits; |
| 85 | BROTLI_UNUSED(one_shot); |
| 86 | BROTLI_UNUSED(input_size); |
| 87 | alloc_size[0] = sizeof(uint16_t) * bucket_size; |
| 88 | alloc_size[1] = sizeof(uint8_t) * bucket_size * block_size; |
| 89 | alloc_size[2] = sizeof(uint32_t) * bucket_size * block_size; |
| 90 | } |
| 91 | /* Look at 4 bytes at &data[ix & mask]. |
| 92 | Compute a hash from these, and store the value of ix at that position. */ |
| 93 | static BROTLI_INLINE void FN(Store)( |
| 94 | HashLongestMatch* BROTLI_RESTRICT self, const uint8_t* BROTLI_RESTRICT data, |
| 95 | const size_t mask, const size_t ix) { |
| 96 | uint16_t* BROTLI_RESTRICT num = self->num_; |
| 97 | uint8_t* BROTLI_RESTRICT tags = self->tags_; |
| 98 | uint32_t* BROTLI_RESTRICT buckets = self->buckets_; |
| 99 | const size_t hash = FN(HashBytes)(&data[ix & mask], self->hash_shift_); |
| 100 | const size_t key = hash >> TAG_HASH_BITS; |
| 101 | const uint8_t tag = hash & TAG_HASH_MASK; |
| 102 | const size_t minor_ix = num[key] & self->block_mask_; |
| 103 | const size_t offset = minor_ix + (key << self->block_bits_); |
| 104 | --num[key]; |
| 105 | buckets[offset] = (uint32_t)ix; |
| 106 | tags[offset] = tag; |
| 107 | } |
| 108 | static BROTLI_INLINE void FN(StoreRange)(HashLongestMatch* BROTLI_RESTRICT self, |
| 109 | const uint8_t* BROTLI_RESTRICT data, const size_t mask, |
| 110 | const size_t ix_start, const size_t ix_end) { |
| 111 | size_t i; |
| 112 | for (i = ix_start; i < ix_end; ++i) { |
| 113 | FN(Store)(self, data, mask, i); |
| 114 | } |
| 115 | } |
| 116 | static BROTLI_INLINE void FN(StitchToPreviousBlock)( |
| 117 | HashLongestMatch* BROTLI_RESTRICT self, |
| 118 | size_t num_bytes, size_t position, const uint8_t* ringbuffer, |
| 119 | size_t ringbuffer_mask) { |
| 120 | if (num_bytes >= FN(HashTypeLength)() - 1 && position >= 3) { |
| 121 | /* Prepare the hashes for three last bytes of the last write. |
| 122 | These could not be calculated before, since they require knowledge |
| 123 | of both the previous and the current block. */ |
| 124 | FN(Store)(self, ringbuffer, ringbuffer_mask, position - 3); |
| 125 | FN(Store)(self, ringbuffer, ringbuffer_mask, position - 2); |
| 126 | FN(Store)(self, ringbuffer, ringbuffer_mask, position - 1); |
| 127 | } |
| 128 | } |
| 129 | static BROTLI_INLINE void FN(PrepareDistanceCache)( |
| 130 | HashLongestMatch* BROTLI_RESTRICT self, |
| 131 | int* BROTLI_RESTRICT distance_cache) { |
| 132 | PrepareDistanceCache(distance_cache, self->num_last_distances_to_check_); |
| 133 | } |
| 134 | |
| 135 | /* Find a longest backward match of &data[cur_ix] up to the length of |
| 136 | max_length and stores the position cur_ix in the hash table. |
| 137 | REQUIRES: FN(PrepareDistanceCache) must be invoked for current distance cache |
| 138 | values; if this method is invoked repeatedly with the same distance |
| 139 | cache values, it is enough to invoke FN(PrepareDistanceCache) once. |
| 140 | Does not look for matches longer than max_length. |
| 141 | Does not look for matches further away than max_backward. |
| 142 | Writes the best match into |out|. |
| 143 | |out|->score is updated only if a better match is found. */ |
| 144 | static BROTLI_INLINE void FN(FindLongestMatch)( |
| 145 | HashLongestMatch* BROTLI_RESTRICT self, |
| 146 | const BrotliEncoderDictionary* dictionary, |
| 147 | const uint8_t* BROTLI_RESTRICT data, const size_t ring_buffer_mask, |
| 148 | const int* BROTLI_RESTRICT distance_cache, const size_t cur_ix, |
| 149 | const size_t max_length, const size_t max_backward, |
| 150 | const size_t dictionary_distance, const size_t max_distance, |
| 151 | HasherSearchResult* BROTLI_RESTRICT out) { |
| 152 | uint16_t* BROTLI_RESTRICT num = self->num_; |
| 153 | uint32_t* BROTLI_RESTRICT buckets = self->buckets_; |
| 154 | uint8_t* BROTLI_RESTRICT tags = self->tags_; |
| 155 | const size_t cur_ix_masked = cur_ix & ring_buffer_mask; |
| 156 | /* Don't accept a short copy from far away. */ |
| 157 | score_t min_score = out->score; |
| 158 | score_t best_score = out->score; |
| 159 | size_t best_len = out->len; |
| 160 | size_t i; |
| 161 | /* Precalculate the hash key and prefetch the bucket. */ |
| 162 | const uint32_t hash = |
| 163 | FN(HashBytes)(&data[cur_ix_masked], self->hash_shift_); |
| 164 | const uint32_t key = hash >> TAG_HASH_BITS; |
| 165 | uint32_t* BROTLI_RESTRICT bucket = &buckets[key << self->block_bits_]; |
| 166 | uint8_t* BROTLI_RESTRICT tag_bucket = &tags[key << self->block_bits_]; |
| 167 | PREFETCH_L1(bucket); |
| 168 | PREFETCH_L1(tag_bucket); |
| 169 | if (self->block_bits_ > 4) PREFETCH_L1(bucket + 16); |
| 170 | out->len = 0; |
| 171 | out->len_code_delta = 0; |
| 172 | |
| 173 | BROTLI_DCHECK(cur_ix_masked + max_length <= ring_buffer_mask); |
| 174 | |
| 175 | /* Try last distance first. */ |
| 176 | for (i = 0; i < (size_t)self->num_last_distances_to_check_; ++i) { |
| 177 | const size_t backward = (size_t)distance_cache[i]; |
| 178 | size_t prev_ix = (size_t)(cur_ix - backward); |
| 179 | if (prev_ix >= cur_ix) { |
| 180 | continue; |
| 181 | } |
| 182 | if (BROTLI_PREDICT_FALSE(backward > max_backward)) { |
| 183 | continue; |
| 184 | } |
| 185 | prev_ix &= ring_buffer_mask; |
| 186 | |
| 187 | if (cur_ix_masked + best_len > ring_buffer_mask) { |
| 188 | break; |
| 189 | } |
| 190 | if (prev_ix + best_len > ring_buffer_mask || |
| 191 | data[cur_ix_masked + best_len] != data[prev_ix + best_len]) { |
| 192 | continue; |
| 193 | } |
| 194 | { |
| 195 | const size_t len = FindMatchLengthWithLimit(&data[prev_ix], |
| 196 | &data[cur_ix_masked], |
| 197 | max_length); |
| 198 | if (len >= 3 || (len == 2 && i < 2)) { |
| 199 | /* Comparing for >= 2 does not change the semantics, but just saves for |
| 200 | a few unnecessary binary logarithms in backward reference score, |
| 201 | since we are not interested in such short matches. */ |
| 202 | score_t score = BackwardReferenceScoreUsingLastDistance(len); |
| 203 | if (best_score < score) { |
| 204 | if (i != 0) score -= BackwardReferencePenaltyUsingLastDistance(i); |
| 205 | if (best_score < score) { |
| 206 | best_score = score; |
| 207 | best_len = len; |
| 208 | out->len = best_len; |
| 209 | out->distance = backward; |
| 210 | out->score = best_score; |
| 211 | } |
| 212 | } |
| 213 | } |
| 214 | } |
| 215 | } |
| 216 | /* we require matches of len >4, so increase best_len to 3, so we can compare |
| 217 | * 4 bytes all the time. */ |
| 218 | if (best_len < 3) { |
| 219 | best_len = 3; |
| 220 | } |
| 221 | { |
| 222 | const uint8_t tag = hash & TAG_HASH_MASK; |
| 223 | const size_t head = (num[key] + 1) & self->block_mask_; |
| 224 | uint64_t matches = |
| 225 | GetMatchingTagMask(self->block_size_ / 16, tag, tag_bucket, head); |
| 226 | /* Mask off any matches from uninitialized tags. */ |
| 227 | uint16_t n = 65535 - num[key]; |
| 228 | uint64_t block_has_unused_slots = self->block_size_ > n; |
| 229 | uint64_t mask = (block_has_unused_slots << (n & (64 - 1))) - 1; |
| 230 | matches &= mask; |
| 231 | for (; matches > 0; matches &= (matches - 1)) { |
| 232 | const size_t rb_index = |
| 233 | (head + (size_t)BROTLI_TZCNT64(matches)) & self->block_mask_; |
| 234 | size_t prev_ix = bucket[rb_index]; |
| 235 | const size_t backward = cur_ix - prev_ix; |
| 236 | if (BROTLI_PREDICT_FALSE(backward > max_backward)) { |
| 237 | break; |
| 238 | } |
| 239 | prev_ix &= ring_buffer_mask; |
| 240 | if (cur_ix_masked + best_len > ring_buffer_mask) { |
| 241 | break; |
| 242 | } |
| 243 | if (prev_ix + best_len > ring_buffer_mask || |
| 244 | /* compare 4 bytes ending at best_len + 1 */ |
| 245 | BrotliUnalignedRead32(&data[cur_ix_masked + best_len - 3]) != |
| 246 | BrotliUnalignedRead32(&data[prev_ix + best_len - 3])) { |
| 247 | continue; |
| 248 | } |
| 249 | { |
| 250 | const size_t len = FindMatchLengthWithLimit(&data[prev_ix], |
| 251 | &data[cur_ix_masked], |
| 252 | max_length); |
| 253 | if (len >= 4) { |
| 254 | /* Comparing for >= 3 does not change the semantics, but just saves |
| 255 | for a few unnecessary binary logarithms in backward reference |
| 256 | score, since we are not interested in such short matches. */ |
| 257 | score_t score = BackwardReferenceScore(len, backward); |
| 258 | if (best_score < score) { |
| 259 | best_score = score; |
| 260 | best_len = len; |
| 261 | out->len = best_len; |
| 262 | out->distance = backward; |
| 263 | out->score = best_score; |
| 264 | } |
| 265 | } |
| 266 | } |
| 267 | } |
| 268 | bucket[num[key] & self->block_mask_] = (uint32_t)cur_ix; |
| 269 | tag_bucket[num[key] & self->block_mask_] = tag; |
| 270 | --num[key]; |
| 271 | } |
| 272 | if (min_score == out->score) { |
| 273 | SearchInStaticDictionary(dictionary, |
| 274 | self->common_, &data[cur_ix_masked], max_length, dictionary_distance, |
| 275 | max_distance, out, BROTLI_FALSE); |
| 276 | } |
| 277 | } |
| 278 | #undef HashLongestMatch |