12#define HashLongestMatchQuickly HASHER()
14#define BUCKET_SIZE (1 << BUCKET_BITS)
15#define BUCKET_MASK (BUCKET_SIZE - 1)
16#define BUCKET_SWEEP (1 << BUCKET_SWEEP_BITS)
17#define BUCKET_SWEEP_MASK ((BUCKET_SWEEP - 1) << 3)
19static BROTLI_INLINE
size_t FN(HashTypeLength)(void) {
return 8; }
20static BROTLI_INLINE
size_t FN(StoreLookahead)(void) {
return 8; }
25static uint32_t FN(HashBytes)(
const uint8_t* data) {
26 const uint64_t h = ((BROTLI_UNALIGNED_LOAD64LE(data) << (64 - 8 * HASH_LEN)) *
30 return (uint32_t)(h >> (64 - BUCKET_BITS));
46static void FN(Initialize)(
49 self->common = common;
51 BROTLI_UNUSED(params);
52 self->buckets_ = (uint32_t*)common->extra[0];
55static void FN(Prepare)(
57 size_t input_size,
const uint8_t* BROTLI_RESTRICT data) {
58 uint32_t* BROTLI_RESTRICT buckets = self->buckets_;
60 size_t partial_prepare_threshold = BUCKET_SIZE >> 5;
61 if (one_shot && input_size <= partial_prepare_threshold) {
63 for (i = 0; i < input_size; ++i) {
64 const uint32_t
key = FN(HashBytes)(&data[i]);
65 if (BUCKET_SWEEP == 1) {
69 for (j = 0; j < BUCKET_SWEEP; ++j) {
70 buckets[(
key + (j << 3)) & BUCKET_MASK] = 0;
79 memset(buckets, 0,
sizeof(uint32_t) * BUCKET_SIZE);
83static BROTLI_INLINE
void FN(HashMemAllocInBytes)(
85 size_t input_size,
size_t* alloc_size) {
86 BROTLI_UNUSED(params);
87 BROTLI_UNUSED(one_shot);
88 BROTLI_UNUSED(input_size);
89 alloc_size[0] =
sizeof(uint32_t) * BUCKET_SIZE;
95static BROTLI_INLINE
void FN(Store)(
97 const uint8_t* BROTLI_RESTRICT data,
const size_t mask,
const size_t ix) {
98 const uint32_t
key = FN(HashBytes)(&data[ix & mask]);
99 if (BUCKET_SWEEP == 1) {
100 self->buckets_[
key] = (uint32_t)ix;
103 const uint32_t off = ix & BUCKET_SWEEP_MASK;
104 self->buckets_[(
key + off) & BUCKET_MASK] = (uint32_t)ix;
108static BROTLI_INLINE
void FN(StoreRange)(
110 const uint8_t* BROTLI_RESTRICT data,
const size_t mask,
111 const size_t ix_start,
const size_t ix_end) {
113 for (i = ix_start; i < ix_end; ++i) {
114 FN(Store)(self, data, mask, i);
118static BROTLI_INLINE
void FN(StitchToPreviousBlock)(
120 size_t num_bytes,
size_t position,
121 const uint8_t* ringbuffer,
size_t ringbuffer_mask) {
122 if (num_bytes >= FN(HashTypeLength)() - 1 && position >= 3) {
126 FN(Store)(self, ringbuffer, ringbuffer_mask, position - 3);
127 FN(Store)(self, ringbuffer, ringbuffer_mask, position - 2);
128 FN(Store)(self, ringbuffer, ringbuffer_mask, position - 1);
132static BROTLI_INLINE
void FN(PrepareDistanceCache)(
134 int* BROTLI_RESTRICT distance_cache) {
136 BROTLI_UNUSED(distance_cache);
147static BROTLI_INLINE
void FN(FindLongestMatch)(
150 const uint8_t* BROTLI_RESTRICT data,
151 const size_t ring_buffer_mask,
const int* BROTLI_RESTRICT distance_cache,
152 const size_t cur_ix,
const size_t max_length,
const size_t max_backward,
153 const size_t dictionary_distance,
const size_t max_distance,
155 uint32_t* BROTLI_RESTRICT buckets = self->buckets_;
156 const size_t best_len_in = out->len;
157 const size_t cur_ix_masked = cur_ix & ring_buffer_mask;
159 int compare_char = data[cur_ix_masked + best_len_in];
160 size_t key = FN(HashBytes)(&data[cur_ix_masked]);
162 score_t min_score = out->score;
163 score_t best_score = out->score;
164 size_t best_len = best_len_in;
165 size_t cached_backward = (size_t)distance_cache[0];
166 size_t prev_ix = cur_ix - cached_backward;
168 BROTLI_DCHECK(cur_ix_masked + max_length <= ring_buffer_mask);
170 out->len_code_delta = 0;
171 if (prev_ix < cur_ix) {
172 prev_ix &= (uint32_t)ring_buffer_mask;
173 if (compare_char == data[prev_ix + best_len]) {
174 const size_t len = FindMatchLengthWithLimit(
175 &data[prev_ix], &data[cur_ix_masked], max_length);
177 const score_t score = BackwardReferenceScoreUsingLastDistance(len);
178 if (best_score < score) {
180 out->distance = cached_backward;
182 if (BUCKET_SWEEP == 1) {
183 buckets[
key] = (uint32_t)cur_ix;
188 compare_char = data[cur_ix_masked + len];
194 if (BUCKET_SWEEP == 1) {
198 prev_ix = buckets[
key];
199 buckets[
key] = (uint32_t)cur_ix;
200 backward = cur_ix - prev_ix;
201 prev_ix &= (uint32_t)ring_buffer_mask;
202 if (compare_char != data[prev_ix + best_len_in]) {
205 if (BROTLI_PREDICT_FALSE(backward == 0 || backward > max_backward)) {
208 len = FindMatchLengthWithLimit(&data[prev_ix],
209 &data[cur_ix_masked],
212 const score_t score = BackwardReferenceScore(len, backward);
213 if (best_score < score) {
215 out->distance = backward;
221 size_t keys[BUCKET_SWEEP];
223 for (i = 0; i < BUCKET_SWEEP; ++i) {
224 keys[i] = (
key + (i << 3)) & BUCKET_MASK;
226 key_out = keys[(cur_ix & BUCKET_SWEEP_MASK) >> 3];
227 for (i = 0; i < BUCKET_SWEEP; ++i) {
230 prev_ix = buckets[keys[i]];
231 backward = cur_ix - prev_ix;
232 prev_ix &= (uint32_t)ring_buffer_mask;
233 if (compare_char != data[prev_ix + best_len]) {
236 if (BROTLI_PREDICT_FALSE(backward == 0 || backward > max_backward)) {
239 len = FindMatchLengthWithLimit(&data[prev_ix],
240 &data[cur_ix_masked],
243 const score_t score = BackwardReferenceScore(len, backward);
244 if (best_score < score) {
247 compare_char = data[cur_ix_masked + len];
250 out->distance = backward;
255 if (USE_DICTIONARY && min_score == out->score) {
256 SearchInStaticDictionary(dictionary,
257 self->common, &data[cur_ix_masked], max_length, dictionary_distance,
260 if (BUCKET_SWEEP != 1) {
261 buckets[key_out] = (uint32_t)cur_ix;
265#undef BUCKET_SWEEP_MASK
270#undef HashLongestMatchQuickly
Definition: encoder_dict.h:61
Definition: hash_longest_match_quickly_inc.h:37
#define BROTLI_TRUE
Portable true replacement.
Definition: types.h:51
#define BROTLI_BOOL
A portable bool replacement.
Definition: types.h:49