18#define HashForgetfulChain HASHER()
20#define BANK_SIZE (1 << BANK_BITS)
23#define BUCKET_SIZE (1 << BUCKET_BITS)
25#define CAPPED_CHAINS 0
27static BROTLI_INLINE
size_t FN(HashTypeLength)(void) {
return 4; }
28static BROTLI_INLINE
size_t FN(StoreLookahead)(void) {
return 4; }
31static BROTLI_INLINE
size_t FN(HashBytes)(
const uint8_t* BROTLI_RESTRICT data) {
32 const uint32_t h = BROTLI_UNALIGNED_LOAD32LE(data) * kHashMul32;
35 return h >> (32 - BUCKET_BITS);
38typedef struct FN(Slot) {
43typedef struct FN(Bank) {
44 FN(Slot) slots[BANK_SIZE];
48 uint16_t free_slot_idx[NUM_BANKS];
67static uint32_t* FN(Addr)(
void* extra) {
68 return (uint32_t*)extra;
71static uint16_t* FN(Head)(
void* extra) {
72 return (uint16_t*)(&FN(Addr)(extra)[BUCKET_SIZE]);
75static uint8_t* FN(TinyHash)(
void* extra) {
76 return (uint8_t*)(&FN(Head)(extra)[BUCKET_SIZE]);
79static FN(Bank)* FN(Banks)(
void* extra) {
80 return (FN(Bank)*)(extra);
83static void FN(Initialize)(
86 self->common = common;
87 self->extra[0] = common->extra[0];
88 self->extra[1] = common->extra[1];
90 self->max_hops = (params->quality > 6 ? 7u : 8u) << (params->quality - 4);
93static void FN(Prepare)(
95 size_t input_size,
const uint8_t* BROTLI_RESTRICT data) {
96 uint32_t* BROTLI_RESTRICT addr = FN(Addr)(self->extra[0]);
97 uint16_t* BROTLI_RESTRICT head = FN(Head)(self->extra[0]);
98 uint8_t* BROTLI_RESTRICT tiny_hash = FN(TinyHash)(self->extra[0]);
100 size_t partial_prepare_threshold = BUCKET_SIZE >> 6;
101 if (one_shot && input_size <= partial_prepare_threshold) {
103 for (i = 0; i < input_size; ++i) {
104 size_t bucket = FN(HashBytes)(&data[i]);
106 addr[bucket] = 0xCCCCCCCC;
107 head[bucket] = 0xCCCC;
113 memset(addr, 0xCC,
sizeof(uint32_t) * BUCKET_SIZE);
114 memset(head, 0,
sizeof(uint16_t) * BUCKET_SIZE);
116 memset(tiny_hash, 0,
sizeof(uint8_t) * 65536);
117 memset(self->free_slot_idx, 0,
sizeof(self->free_slot_idx));
120static BROTLI_INLINE
void FN(HashMemAllocInBytes)(
122 size_t input_size,
size_t* alloc_size) {
123 BROTLI_UNUSED(params);
124 BROTLI_UNUSED(one_shot);
125 BROTLI_UNUSED(input_size);
126 alloc_size[0] =
sizeof(uint32_t) * BUCKET_SIZE +
127 sizeof(uint16_t) * BUCKET_SIZE +
sizeof(uint8_t) * 65536;
128 alloc_size[1] =
sizeof(FN(Bank)) * NUM_BANKS;
134 const uint8_t* BROTLI_RESTRICT data,
const size_t mask,
const size_t ix) {
135 uint32_t* BROTLI_RESTRICT addr = FN(Addr)(self->extra[0]);
136 uint16_t* BROTLI_RESTRICT head = FN(Head)(self->extra[0]);
137 uint8_t* BROTLI_RESTRICT tiny_hash = FN(TinyHash)(self->extra[0]);
138 FN(Bank)* BROTLI_RESTRICT banks = FN(Banks)(self->extra[1]);
139 const size_t key = FN(HashBytes)(&data[ix & mask]);
140 const size_t bank =
key & (NUM_BANKS - 1);
141 const size_t idx = self->free_slot_idx[bank]++ & (BANK_SIZE - 1);
142 size_t delta = ix - addr[
key];
143 tiny_hash[(uint16_t)ix] = (uint8_t)
key;
144 if (delta > 0xFFFF) delta = CAPPED_CHAINS ? 0 : 0xFFFF;
145 banks[bank].slots[idx].delta = (uint16_t)delta;
146 banks[bank].slots[idx].next = head[
key];
147 addr[
key] = (uint32_t)ix;
148 head[
key] = (uint16_t)idx;
151static BROTLI_INLINE
void FN(StoreRange)(
153 const uint8_t* BROTLI_RESTRICT data,
const size_t mask,
154 const size_t ix_start,
const size_t ix_end) {
156 for (i = ix_start; i < ix_end; ++i) {
157 FN(Store)(self, data, mask, i);
161static BROTLI_INLINE
void FN(StitchToPreviousBlock)(
163 size_t num_bytes,
size_t position,
const uint8_t* ringbuffer,
164 size_t ring_buffer_mask) {
165 if (num_bytes >= FN(HashTypeLength)() - 1 && position >= 3) {
169 FN(Store)(self, ringbuffer, ring_buffer_mask, position - 3);
170 FN(Store)(self, ringbuffer, ring_buffer_mask, position - 2);
171 FN(Store)(self, ringbuffer, ring_buffer_mask, position - 1);
175static BROTLI_INLINE
void FN(PrepareDistanceCache)(
177 int* BROTLI_RESTRICT distance_cache) {
179 PrepareDistanceCache(distance_cache, NUM_LAST_DISTANCES_TO_CHECK);
193static BROTLI_INLINE
void FN(FindLongestMatch)(
196 const uint8_t* BROTLI_RESTRICT data,
const size_t ring_buffer_mask,
197 const int* BROTLI_RESTRICT distance_cache,
198 const size_t cur_ix,
const size_t max_length,
const size_t max_backward,
199 const size_t dictionary_distance,
const size_t max_distance,
201 uint32_t* BROTLI_RESTRICT addr = FN(Addr)(self->extra[0]);
202 uint16_t* BROTLI_RESTRICT head = FN(Head)(self->extra[0]);
203 uint8_t* BROTLI_RESTRICT tiny_hashes = FN(TinyHash)(self->extra[0]);
204 FN(Bank)* BROTLI_RESTRICT banks = FN(Banks)(self->extra[1]);
205 const size_t cur_ix_masked = cur_ix & ring_buffer_mask;
207 score_t min_score = out->score;
208 score_t best_score = out->score;
209 size_t best_len = out->len;
211 const size_t key = FN(HashBytes)(&data[cur_ix_masked]);
212 const uint8_t tiny_hash = (uint8_t)(
key);
214 out->len_code_delta = 0;
216 BROTLI_DCHECK(cur_ix_masked + max_length <= ring_buffer_mask);
219 for (i = 0; i < NUM_LAST_DISTANCES_TO_CHECK; ++i) {
220 const size_t backward = (size_t)distance_cache[i];
221 size_t prev_ix = (cur_ix - backward);
223 if (i > 0 && tiny_hashes[(uint16_t)prev_ix] != tiny_hash)
continue;
224 if (prev_ix >= cur_ix || backward > max_backward) {
227 prev_ix &= ring_buffer_mask;
229 const size_t len = FindMatchLengthWithLimit(&data[prev_ix],
230 &data[cur_ix_masked],
233 score_t score = BackwardReferenceScoreUsingLastDistance(len);
234 if (best_score < score) {
235 if (i != 0) score -= BackwardReferencePenaltyUsingLastDistance(i);
236 if (best_score < score) {
240 out->distance = backward;
241 out->score = best_score;
253 const size_t bank =
key & (NUM_BANKS - 1);
255 size_t hops = self->max_hops;
256 size_t delta = cur_ix - addr[
key];
257 size_t slot = head[
key];
262 if (backward > max_backward || (CAPPED_CHAINS && !delta))
break;
263 prev_ix = (cur_ix - backward) & ring_buffer_mask;
264 slot = banks[bank].slots[last].next;
265 delta = banks[bank].slots[last].delta;
266 if (cur_ix_masked + best_len > ring_buffer_mask ||
267 prev_ix + best_len > ring_buffer_mask ||
269 BrotliUnalignedRead32(&data[cur_ix_masked + best_len - 3]) !=
270 BrotliUnalignedRead32(&data[prev_ix + best_len - 3])) {
274 const size_t len = FindMatchLengthWithLimit(&data[prev_ix],
275 &data[cur_ix_masked],
281 score_t score = BackwardReferenceScore(len, backward);
282 if (best_score < score) {
286 out->distance = backward;
287 out->score = best_score;
292 FN(Store)(self, data, ring_buffer_mask, cur_ix);
294 if (out->score == min_score) {
295 SearchInStaticDictionary(dictionary,
296 self->common, &data[cur_ix_masked], max_length, dictionary_distance,
305#undef HashForgetfulChain
Definition: encoder_dict.h:61
Definition: hash_forgetful_chain_inc.h:47
#define BROTLI_FALSE
Portable false replacement.
Definition: types.h:53
#define BROTLI_BOOL
A portable bool replacement.
Definition: types.h:49