17#define HashLongestMatch HASHER()
19#define TAG_HASH_BITS 8
20#define TAG_HASH_MASK ((1 << TAG_HASH_BITS) - 1)
22static BROTLI_INLINE
size_t FN(HashTypeLength)(void) {
return 8; }
23static BROTLI_INLINE
size_t FN(StoreLookahead)(void) {
return 8; }
26static BROTLI_INLINE
size_t FN(HashBytes)(
const uint8_t* BROTLI_RESTRICT data,
28 const uint64_t h = BROTLI_UNALIGNED_LOAD64LE(data) * hash_mul;
31 return (
size_t)(h >> (64 - 15 - TAG_HASH_BITS));
46 int num_last_distances_to_check_;
62static void FN(Initialize)(
65 self->common_ = common;
67 BROTLI_UNUSED(params);
68 self->hash_mul_ = kHashMul64 << (64 - 5 * 8);
69 BROTLI_DCHECK(common->params.bucket_bits == 15);
70 self->bucket_size_ = (size_t)1 << common->params.bucket_bits;
71 self->block_bits_ = common->params.block_bits;
72 self->block_size_ = (
size_t)1 << common->params.block_bits;
73 self->block_mask_ = (uint32_t)(self->block_size_ - 1);
74 self->num_last_distances_to_check_ =
75 common->params.num_last_distances_to_check;
76 self->num_ = (uint16_t*)common->extra[0];
77 self->tags_ = (uint8_t*)common->extra[1];
78 self->buckets_ = (uint32_t*)common->extra[2];
81static void FN(Prepare)(
83 size_t input_size,
const uint8_t* BROTLI_RESTRICT data) {
84 uint16_t* BROTLI_RESTRICT num = self->num_;
86 size_t partial_prepare_threshold = self->bucket_size_ >> 6;
87 if (one_shot && input_size <= partial_prepare_threshold) {
89 for (i = 0; i < input_size; ++i) {
90 const size_t hash = FN(HashBytes)(&data[i], self->hash_mul_);
91 const size_t key =
hash >> TAG_HASH_BITS;
96 memset(num, 255, self->bucket_size_ *
sizeof(num[0]));
100static BROTLI_INLINE
void FN(HashMemAllocInBytes)(
102 size_t input_size,
size_t* alloc_size) {
103 size_t bucket_size = (size_t)1 << params->hasher.bucket_bits;
104 size_t block_size = (
size_t)1 << params->hasher.block_bits;
105 BROTLI_UNUSED(one_shot);
106 BROTLI_UNUSED(input_size);
107 alloc_size[0] =
sizeof(uint16_t) * bucket_size;
108 alloc_size[1] =
sizeof(uint8_t) * bucket_size * block_size;
109 alloc_size[2] =
sizeof(uint32_t) * bucket_size * block_size;
114static BROTLI_INLINE
void FN(Store)(
115 HashLongestMatch* BROTLI_RESTRICT self,
const uint8_t* BROTLI_RESTRICT data,
116 const size_t mask,
const size_t ix) {
117 uint16_t* BROTLI_RESTRICT num = self->num_;
118 uint8_t* BROTLI_RESTRICT tags = self->tags_;
119 uint32_t* BROTLI_RESTRICT buckets = self->buckets_;
120 const size_t hash = FN(HashBytes)(&data[ix & mask], self->hash_mul_);
121 const size_t key =
hash >> TAG_HASH_BITS;
122 const uint8_t tag =
hash & TAG_HASH_MASK;
123 const size_t minor_ix = num[
key] & self->block_mask_;
124 const size_t offset = minor_ix + (
key << self->block_bits_);
126 buckets[offset] = (uint32_t)ix;
130static BROTLI_INLINE
void FN(StoreRange)(
HashLongestMatch* BROTLI_RESTRICT self,
131 const uint8_t* BROTLI_RESTRICT data,
const size_t mask,
132 const size_t ix_start,
const size_t ix_end) {
134 for (i = ix_start; i < ix_end; ++i) {
135 FN(Store)(self, data, mask, i);
139static BROTLI_INLINE
void FN(StitchToPreviousBlock)(
141 size_t num_bytes,
size_t position,
const uint8_t* ringbuffer,
142 size_t ringbuffer_mask) {
143 if (num_bytes >= FN(HashTypeLength)() - 1 && position >= 3) {
147 FN(Store)(self, ringbuffer, ringbuffer_mask, position - 3);
148 FN(Store)(self, ringbuffer, ringbuffer_mask, position - 2);
149 FN(Store)(self, ringbuffer, ringbuffer_mask, position - 1);
153static BROTLI_INLINE
void FN(PrepareDistanceCache)(
155 int* BROTLI_RESTRICT distance_cache) {
156 PrepareDistanceCache(distance_cache, self->num_last_distances_to_check_);
170static BROTLI_INLINE
void FN(FindLongestMatch)(
173 const uint8_t* BROTLI_RESTRICT data,
const size_t ring_buffer_mask,
174 const int* BROTLI_RESTRICT distance_cache,
const size_t cur_ix,
175 const size_t max_length,
const size_t max_backward,
176 const size_t dictionary_distance,
const size_t max_distance,
178 uint16_t* BROTLI_RESTRICT num = self->num_;
179 uint32_t* BROTLI_RESTRICT buckets = self->buckets_;
180 uint8_t* BROTLI_RESTRICT tags = self->tags_;
181 const size_t cur_ix_masked = cur_ix & ring_buffer_mask;
183 score_t min_score = out->score;
184 score_t best_score = out->score;
185 size_t best_len = out->len;
188 const size_t hash = FN(HashBytes)(&data[cur_ix_masked], self->hash_mul_);
189 const size_t key =
hash >> TAG_HASH_BITS;
190 uint32_t* BROTLI_RESTRICT bucket = &buckets[
key << self->block_bits_];
191 uint8_t* BROTLI_RESTRICT tag_bucket = &tags[
key << self->block_bits_];
193 PREFETCH_L1(tag_bucket);
194 if (self->block_bits_ > 4) PREFETCH_L1(bucket + 16);
196 out->len_code_delta = 0;
198 BROTLI_DCHECK(cur_ix_masked + max_length <= ring_buffer_mask);
201 for (i = 0; i < (size_t)self->num_last_distances_to_check_; ++i) {
202 const size_t backward = (size_t)distance_cache[i];
203 size_t prev_ix = (size_t)(cur_ix - backward);
204 if (prev_ix >= cur_ix) {
207 if (BROTLI_PREDICT_FALSE(backward > max_backward)) {
210 prev_ix &= ring_buffer_mask;
212 if (cur_ix_masked + best_len > ring_buffer_mask) {
215 if (prev_ix + best_len > ring_buffer_mask ||
216 data[cur_ix_masked + best_len] != data[prev_ix + best_len]) {
220 const size_t len = FindMatchLengthWithLimit(&data[prev_ix],
221 &data[cur_ix_masked],
223 if (len >= 3 || (len == 2 && i < 2)) {
227 score_t score = BackwardReferenceScoreUsingLastDistance(len);
228 if (best_score < score) {
229 if (i != 0) score -= BackwardReferencePenaltyUsingLastDistance(i);
230 if (best_score < score) {
234 out->distance = backward;
235 out->score = best_score;
247 const uint8_t tag =
hash & TAG_HASH_MASK;
248 const uint32_t first4 = BrotliUnalignedRead32(data + cur_ix_masked);
249 const size_t max_length_m4 = max_length - 4;
250 const size_t head = (num[
key] + 1) & self->block_mask_;
252 GetMatchingTagMask(self->block_size_ / 16, tag, tag_bucket, head);
254 uint16_t n = 65535 - num[
key];
255 uint64_t block_has_unused_slots = self->block_size_ > n;
256 uint64_t mask = (block_has_unused_slots << (n & (64 - 1))) - 1;
258 for (; matches > 0; matches &= (matches - 1)) {
259 const size_t rb_index =
260 (head + (size_t)BROTLI_TZCNT64(matches)) & self->block_mask_;
261 size_t prev_ix = bucket[rb_index];
263 const size_t backward = cur_ix - prev_ix;
264 if (BROTLI_PREDICT_FALSE(backward > max_backward)) {
267 prev_ix &= ring_buffer_mask;
268 if (cur_ix_masked + best_len > ring_buffer_mask) {
271 if (prev_ix + best_len > ring_buffer_mask ||
273 BrotliUnalignedRead32(&data[cur_ix_masked + best_len - 3]) !=
274 BrotliUnalignedRead32(&data[prev_ix + best_len - 3])) {
277 current4 = BrotliUnalignedRead32(data + prev_ix);
278 if (first4 != current4)
continue;
280 const size_t len = FindMatchLengthWithLimit(&data[prev_ix + 4],
281 &data[cur_ix_masked + 4],
283 const score_t score = BackwardReferenceScore(len, backward);
284 if (best_score < score) {
288 out->distance = backward;
289 out->score = best_score;
293 bucket[num[
key] & self->block_mask_] = (uint32_t)cur_ix;
294 tag_bucket[num[
key] & self->block_mask_] = tag;
297 if (min_score == out->score) {
298 SearchInStaticDictionary(dictionary,
299 self->common_, &data[cur_ix_masked], max_length, dictionary_distance,
304#undef HashLongestMatch
std::size_t hash(const BasicJsonType &j)
hash a JSON value
Definition: json.hpp:6022
Definition: encoder_dict.h:61
Definition: hash_longest_match64_inc.h:31
#define BROTLI_FALSE
Portable false replacement.
Definition: types.h:53
#define BROTLI_BOOL
A portable bool replacement.
Definition: types.h:49