18#define HashToBinaryTree HASHER()
20#define BUCKET_SIZE (1 << BUCKET_BITS)
22static BROTLI_INLINE
size_t FN(HashTypeLength)(void) {
return 4; }
23static BROTLI_INLINE
size_t FN(StoreLookahead)(void) {
24 return MAX_TREE_COMP_LENGTH;
27static uint32_t FN(HashBytes)(
const uint8_t* BROTLI_RESTRICT data) {
28 uint32_t h = BROTLI_UNALIGNED_LOAD32LE(data) * kHashMul32;
31 return h >> (32 - BUCKET_BITS);
46 uint32_t invalid_pos_;
57static void FN(Initialize)(
60 self->buckets_ = (uint32_t*)common->extra[0];
61 self->forest_ = (uint32_t*)common->extra[1];
63 self->window_mask_ = (1u << params->lgwin) - 1u;
64 self->invalid_pos_ = (uint32_t)(0 - self->window_mask_);
67static void FN(Prepare)
69 size_t input_size,
const uint8_t* BROTLI_RESTRICT data) {
70 uint32_t invalid_pos = self->invalid_pos_;
72 uint32_t* BROTLI_RESTRICT buckets = self->buckets_;
74 BROTLI_UNUSED(one_shot);
75 BROTLI_UNUSED(input_size);
76 for (i = 0; i < BUCKET_SIZE; i++) {
77 buckets[i] = invalid_pos;
81static BROTLI_INLINE
void FN(HashMemAllocInBytes)(
83 size_t input_size,
size_t* alloc_size) {
84 size_t num_nodes = (size_t)1 << params->lgwin;
85 if (one_shot && input_size < num_nodes) {
86 num_nodes = input_size;
88 alloc_size[0] =
sizeof(uint32_t) * BUCKET_SIZE;
89 alloc_size[1] = 2 *
sizeof(uint32_t) * num_nodes;
92static BROTLI_INLINE
size_t FN(LeftChildIndex)(
95 return 2 * (pos & self->window_mask_);
98static BROTLI_INLINE
size_t FN(RightChildIndex)(
101 return 2 * (pos & self->window_mask_) + 1;
115 HashToBinaryTree* BROTLI_RESTRICT self,
const uint8_t* BROTLI_RESTRICT data,
116 const size_t cur_ix,
const size_t ring_buffer_mask,
const size_t max_length,
117 const size_t max_backward,
size_t*
const BROTLI_RESTRICT best_len,
119 const size_t cur_ix_masked = cur_ix & ring_buffer_mask;
120 const size_t max_comp_len =
121 BROTLI_MIN(
size_t, max_length, MAX_TREE_COMP_LENGTH);
124 const uint32_t
key = FN(HashBytes)(&data[cur_ix_masked]);
125 uint32_t* BROTLI_RESTRICT buckets = self->buckets_;
126 uint32_t* BROTLI_RESTRICT forest = self->forest_;
127 size_t prev_ix = buckets[
key];
130 size_t node_left = FN(LeftChildIndex)(self, cur_ix);
133 size_t node_right = FN(RightChildIndex)(self, cur_ix);
136 size_t best_len_left = 0;
139 size_t best_len_right = 0;
140 size_t depth_remaining;
141 if (should_reroot_tree) {
142 buckets[
key] = (uint32_t)cur_ix;
144 for (depth_remaining = MAX_TREE_SEARCH_DEPTH; ; --depth_remaining) {
145 const size_t backward = cur_ix - prev_ix;
146 const size_t prev_ix_masked = prev_ix & ring_buffer_mask;
147 if (backward == 0 || backward > max_backward || depth_remaining == 0) {
148 if (should_reroot_tree) {
149 forest[node_left] = self->invalid_pos_;
150 forest[node_right] = self->invalid_pos_;
155 const size_t cur_len = BROTLI_MIN(
size_t, best_len_left, best_len_right);
157 BROTLI_DCHECK(cur_len <= MAX_TREE_COMP_LENGTH);
159 FindMatchLengthWithLimit(&data[cur_ix_masked + cur_len],
160 &data[prev_ix_masked + cur_len],
161 max_length - cur_len);
163 0 == memcmp(&data[cur_ix_masked], &data[prev_ix_masked], len));
164 if (matches && len > *best_len) {
166 InitBackwardMatch(matches++, backward, len);
168 if (len >= max_comp_len) {
169 if (should_reroot_tree) {
170 forest[node_left] = forest[FN(LeftChildIndex)(self, prev_ix)];
171 forest[node_right] = forest[FN(RightChildIndex)(self, prev_ix)];
175 if (data[cur_ix_masked + len] > data[prev_ix_masked + len]) {
177 if (should_reroot_tree) {
178 forest[node_left] = (uint32_t)prev_ix;
180 node_left = FN(RightChildIndex)(self, prev_ix);
181 prev_ix = forest[node_left];
183 best_len_right = len;
184 if (should_reroot_tree) {
185 forest[node_right] = (uint32_t)prev_ix;
187 node_right = FN(LeftChildIndex)(self, prev_ix);
188 prev_ix = forest[node_right];
202static BROTLI_INLINE
size_t FN(FindAllMatches)(
205 const uint8_t* BROTLI_RESTRICT data,
206 const size_t ring_buffer_mask,
const size_t cur_ix,
207 const size_t max_length,
const size_t max_backward,
211 const size_t cur_ix_masked = cur_ix & ring_buffer_mask;
213 const size_t short_match_max_backward =
214 params->quality != HQ_ZOPFLIFICATION_QUALITY ? 16 : 64;
215 size_t stop = cur_ix - short_match_max_backward;
216 uint32_t dict_matches[BROTLI_MAX_STATIC_DICTIONARY_MATCH_LEN + 1];
218 if (cur_ix < short_match_max_backward) { stop = 0; }
219 for (i = cur_ix - 1; i > stop && best_len <= 2; --i) {
221 const size_t backward = cur_ix - prev_ix;
222 if (BROTLI_PREDICT_FALSE(backward > max_backward)) {
225 prev_ix &= ring_buffer_mask;
226 if (data[cur_ix_masked] != data[prev_ix] ||
227 data[cur_ix_masked + 1] != data[prev_ix + 1]) {
232 FindMatchLengthWithLimit(&data[prev_ix], &data[cur_ix_masked],
234 if (len > best_len) {
236 InitBackwardMatch(matches++, backward, len);
240 if (best_len < max_length) {
241 matches = FN(StoreAndFindMatches)(self, data, cur_ix,
242 ring_buffer_mask, max_length, max_backward, &best_len, matches);
244 for (i = 0; i <= BROTLI_MAX_STATIC_DICTIONARY_MATCH_LEN; ++i) {
245 dict_matches[i] = kInvalidMatch;
248 size_t minlen = BROTLI_MAX(
size_t, 4, best_len + 1);
249 if (BrotliFindAllStaticDictionaryMatches(dictionary,
250 &data[cur_ix_masked], minlen, max_length, &dict_matches[0])) {
251 size_t maxlen = BROTLI_MIN(
252 size_t, BROTLI_MAX_STATIC_DICTIONARY_MATCH_LEN, max_length);
254 for (l = minlen; l <= maxlen; ++l) {
255 uint32_t dict_id = dict_matches[l];
256 if (dict_id < kInvalidMatch) {
257 size_t distance = dictionary_distance + (dict_id >> 5) + 1;
258 if (distance <= params->dist.max_distance) {
259 InitDictionaryBackwardMatch(matches++, distance, l, dict_id & 31);
265 return (
size_t)(matches - orig_matches);
272 const uint8_t* BROTLI_RESTRICT data,
273 const size_t mask,
const size_t ix) {
275 const size_t max_backward = self->window_mask_ - BROTLI_WINDOW_GAP + 1;
276 FN(StoreAndFindMatches)(self, data, ix, mask, MAX_TREE_COMP_LENGTH,
277 max_backward, NULL, NULL);
280static BROTLI_INLINE
void FN(StoreRange)(
HashToBinaryTree* BROTLI_RESTRICT self,
281 const uint8_t* BROTLI_RESTRICT data,
const size_t mask,
282 const size_t ix_start,
const size_t ix_end) {
285 if (ix_start + 63 <= ix_end) {
288 if (ix_start + 512 <= i) {
289 for (; j < i; j += 8) {
290 FN(Store)(self, data, mask, j);
293 for (; i < ix_end; ++i) {
294 FN(Store)(self, data, mask, i);
298static BROTLI_INLINE
void FN(StitchToPreviousBlock)(
300 size_t num_bytes,
size_t position,
const uint8_t* ringbuffer,
301 size_t ringbuffer_mask) {
302 if (num_bytes >= FN(HashTypeLength)() - 1 &&
303 position >= MAX_TREE_COMP_LENGTH) {
307 const size_t i_start = position - MAX_TREE_COMP_LENGTH + 1;
308 const size_t i_end = BROTLI_MIN(
size_t, position, i_start + num_bytes);
310 for (i = i_start; i < i_end; ++i) {
315 const size_t max_backward =
316 self->window_mask_ - BROTLI_MAX(
size_t,
317 BROTLI_WINDOW_GAP - 1,
322 FN(StoreAndFindMatches)(self, ringbuffer, i, ringbuffer_mask,
323 MAX_TREE_COMP_LENGTH, max_backward, NULL, NULL);
330#undef HashToBinaryTree
Definition: encoder_dict.h:61
Definition: hash_to_binary_tree_inc.h:34
#define TO_BROTLI_BOOL(X)
bool to BROTLI_BOOL conversion macros.
Definition: types.h:55
#define BROTLI_BOOL
A portable bool replacement.
Definition: types.h:49