/src/ghostpdl/brotli/c/enc/hash.h
Line | Count | Source |
1 | | /* Copyright 2010 Google Inc. All Rights Reserved. |
2 | | |
3 | | Distributed under MIT license. |
4 | | See file LICENSE for detail or copy at https://opensource.org/licenses/MIT |
5 | | */ |
6 | | |
7 | | /* A (forgetful) hash table to the data seen by the compressor, to |
8 | | help create backward references to previous data. */ |
9 | | |
10 | | #ifndef BROTLI_ENC_HASH_H_ |
11 | | #define BROTLI_ENC_HASH_H_ |
12 | | |
13 | | #include "../common/constants.h" |
14 | | #include "../common/dictionary.h" |
15 | | #include "../common/platform.h" |
16 | | #include "compound_dictionary.h" |
17 | | #include "encoder_dict.h" |
18 | | #include "fast_log.h" |
19 | | #include "find_match_length.h" |
20 | | #include "hash_base.h" |
21 | | #include "matching_tag_mask.h" |
22 | | #include "memory.h" |
23 | | #include "params.h" |
24 | | #include "quality.h" |
25 | | #include "static_dict.h" |
26 | | |
27 | | #if defined(__cplusplus) || defined(c_plusplus) |
28 | | extern "C" { |
29 | | #endif |
30 | | |
31 | | typedef struct { |
32 | | size_t start_literal_pos; |
33 | | size_t length; |
34 | | } Base64Region; |
35 | | |
36 | | typedef struct { |
37 | | /** |
38 | | * Dynamically allocated areas; regular hasher uses one or two allocations; |
39 | | * "composite" hasher uses up to 4 allocations. |
40 | | */ |
41 | | void* extra[4]; |
42 | | |
43 | | /** |
44 | | * False before the first invocation of HasherSetup (where "extra" memory) |
45 | | * is allocated. |
46 | | */ |
47 | | BROTLI_BOOL is_setup_; |
48 | | |
49 | | size_t dict_num_lookups; |
50 | | size_t dict_num_matches; |
51 | | |
52 | | BrotliHasherParams params; |
53 | | |
54 | | /** |
55 | | * False if hasher needs to be "prepared" before use (before the first |
56 | | * invocation of HasherSetup or after HasherReset). "preparation" is hasher |
57 | | * data initialization (using input ringbuffer). |
58 | | */ |
59 | | BROTLI_BOOL is_prepared_; |
60 | | |
61 | | Base64Region* base64_regions; |
62 | | size_t num_base64_regions; |
63 | | } HasherCommon; |
64 | | |
65 | 0 | #define score_t size_t |
66 | | |
67 | | static const uint32_t kCutoffTransformsCount = 10; |
68 | | /* 0, 12, 27, 23, 42, 63, 56, 48, 59, 64 */ |
69 | | /* 0+0, 4+8, 8+19, 12+11, 16+26, 20+43, 24+32, 28+20, 32+27, 36+28 */ |
70 | | static const uint64_t kCutoffTransforms = |
71 | | BROTLI_MAKE_UINT64_T(0x071B520A, 0xDA2D3200); |
72 | | |
73 | | typedef struct HasherSearchResult { |
74 | | size_t len; |
75 | | size_t distance; |
76 | | score_t score; |
77 | | int len_code_delta; /* == len_code - len */ |
78 | | } HasherSearchResult; |
79 | | |
80 | | static BROTLI_INLINE void PrepareDistanceCache( |
81 | 0 | int* BROTLI_RESTRICT distance_cache, const int num_distances) { |
82 | 0 | if (num_distances > 4) { |
83 | 0 | int last_distance = distance_cache[0]; |
84 | 0 | distance_cache[4] = last_distance - 1; |
85 | 0 | distance_cache[5] = last_distance + 1; |
86 | 0 | distance_cache[6] = last_distance - 2; |
87 | 0 | distance_cache[7] = last_distance + 2; |
88 | 0 | distance_cache[8] = last_distance - 3; |
89 | 0 | distance_cache[9] = last_distance + 3; |
90 | 0 | if (num_distances > 10) { |
91 | 0 | int next_last_distance = distance_cache[1]; |
92 | 0 | distance_cache[10] = next_last_distance - 1; |
93 | 0 | distance_cache[11] = next_last_distance + 1; |
94 | 0 | distance_cache[12] = next_last_distance - 2; |
95 | 0 | distance_cache[13] = next_last_distance + 2; |
96 | 0 | distance_cache[14] = next_last_distance - 3; |
97 | 0 | distance_cache[15] = next_last_distance + 3; |
98 | 0 | } |
99 | 0 | } |
100 | 0 | } Unexecuted instantiation: encode.c:PrepareDistanceCache Unexecuted instantiation: encoder_dict.c:PrepareDistanceCache Unexecuted instantiation: metablock.c:PrepareDistanceCache Unexecuted instantiation: backward_references.c:PrepareDistanceCache Unexecuted instantiation: backward_references_hq.c:PrepareDistanceCache Unexecuted instantiation: brotli_bit_stream.c:PrepareDistanceCache Unexecuted instantiation: compress_fragment.c:PrepareDistanceCache Unexecuted instantiation: compress_fragment_two_pass.c:PrepareDistanceCache |
101 | | |
102 | 0 | #define BROTLI_LITERAL_BYTE_SCORE 135 |
103 | 0 | #define BROTLI_DISTANCE_BIT_PENALTY 30 |
104 | | /* Score must be positive after applying maximal penalty. */ |
105 | 0 | #define BROTLI_SCORE_BASE (BROTLI_DISTANCE_BIT_PENALTY * 8 * sizeof(size_t)) |
106 | | |
107 | | /* Usually, we always choose the longest backward reference. This function |
108 | | allows for the exception of that rule. |
109 | | |
110 | | If we choose a backward reference that is further away, it will |
111 | | usually be coded with more bits. We approximate this by assuming |
112 | | log2(distance). If the distance can be expressed in terms of the |
113 | | last four distances, we use some heuristic constants to estimate |
114 | | the bits cost. For the first up to four literals we use the bit |
115 | | cost of the literals from the literal cost model, after that we |
116 | | use the average bit cost of the cost model. |
117 | | |
118 | | This function is used to sometimes discard a longer backward reference |
119 | | when it is not much longer and the bit cost for encoding it is more |
120 | | than the saved literals. |
121 | | |
122 | | backward_reference_offset MUST be positive. */ |
123 | | static BROTLI_INLINE score_t BackwardReferenceScore( |
124 | 0 | size_t copy_length, size_t backward_reference_offset) { |
125 | 0 | return BROTLI_SCORE_BASE + BROTLI_LITERAL_BYTE_SCORE * (score_t)copy_length - |
126 | 0 | BROTLI_DISTANCE_BIT_PENALTY * Log2FloorNonZero(backward_reference_offset); |
127 | 0 | } Unexecuted instantiation: encode.c:BackwardReferenceScore Unexecuted instantiation: encoder_dict.c:BackwardReferenceScore Unexecuted instantiation: metablock.c:BackwardReferenceScore Unexecuted instantiation: backward_references.c:BackwardReferenceScore Unexecuted instantiation: backward_references_hq.c:BackwardReferenceScore Unexecuted instantiation: brotli_bit_stream.c:BackwardReferenceScore Unexecuted instantiation: compress_fragment.c:BackwardReferenceScore Unexecuted instantiation: compress_fragment_two_pass.c:BackwardReferenceScore |
128 | | |
129 | | /* Returns the minimum length of a backward reference that will improve on the |
130 | | provided score. We conservatively assume that the match will be a last |
131 | | distance match, the best case scenario for the next match.*/ |
132 | 0 | static BROTLI_INLINE size_t MinimumBetterLength(score_t score) { |
133 | 0 | return (score - (BROTLI_SCORE_BASE + 15)) / BROTLI_LITERAL_BYTE_SCORE; |
134 | 0 | } Unexecuted instantiation: encode.c:MinimumBetterLength Unexecuted instantiation: encoder_dict.c:MinimumBetterLength Unexecuted instantiation: metablock.c:MinimumBetterLength Unexecuted instantiation: backward_references.c:MinimumBetterLength Unexecuted instantiation: backward_references_hq.c:MinimumBetterLength Unexecuted instantiation: brotli_bit_stream.c:MinimumBetterLength Unexecuted instantiation: compress_fragment.c:MinimumBetterLength Unexecuted instantiation: compress_fragment_two_pass.c:MinimumBetterLength |
135 | | |
136 | | static BROTLI_INLINE score_t BackwardReferenceScoreUsingLastDistance( |
137 | 0 | size_t copy_length) { |
138 | 0 | return BROTLI_LITERAL_BYTE_SCORE * (score_t)copy_length + |
139 | 0 | BROTLI_SCORE_BASE + 15; |
140 | 0 | } Unexecuted instantiation: encode.c:BackwardReferenceScoreUsingLastDistance Unexecuted instantiation: encoder_dict.c:BackwardReferenceScoreUsingLastDistance Unexecuted instantiation: metablock.c:BackwardReferenceScoreUsingLastDistance Unexecuted instantiation: backward_references.c:BackwardReferenceScoreUsingLastDistance Unexecuted instantiation: backward_references_hq.c:BackwardReferenceScoreUsingLastDistance Unexecuted instantiation: brotli_bit_stream.c:BackwardReferenceScoreUsingLastDistance Unexecuted instantiation: compress_fragment.c:BackwardReferenceScoreUsingLastDistance Unexecuted instantiation: compress_fragment_two_pass.c:BackwardReferenceScoreUsingLastDistance |
141 | | |
142 | | static BROTLI_INLINE score_t BackwardReferencePenaltyUsingLastDistance( |
143 | 0 | size_t distance_short_code) { |
144 | 0 | return (score_t)39 + ((0x1CA10 >> (distance_short_code & 0xE)) & 0xE); |
145 | 0 | } Unexecuted instantiation: encode.c:BackwardReferencePenaltyUsingLastDistance Unexecuted instantiation: encoder_dict.c:BackwardReferencePenaltyUsingLastDistance Unexecuted instantiation: metablock.c:BackwardReferencePenaltyUsingLastDistance Unexecuted instantiation: backward_references.c:BackwardReferencePenaltyUsingLastDistance Unexecuted instantiation: backward_references_hq.c:BackwardReferencePenaltyUsingLastDistance Unexecuted instantiation: brotli_bit_stream.c:BackwardReferencePenaltyUsingLastDistance Unexecuted instantiation: compress_fragment.c:BackwardReferencePenaltyUsingLastDistance Unexecuted instantiation: compress_fragment_two_pass.c:BackwardReferencePenaltyUsingLastDistance |
146 | | |
147 | | static BROTLI_INLINE BROTLI_BOOL TestStaticDictionaryItem( |
148 | | const BrotliEncoderDictionary* dictionary, size_t len, size_t word_idx, |
149 | | const uint8_t* data, size_t max_length, size_t max_backward, |
150 | 0 | size_t max_distance, HasherSearchResult* out) { |
151 | 0 | size_t offset; |
152 | 0 | size_t matchlen; |
153 | 0 | size_t backward; |
154 | 0 | score_t score; |
155 | 0 | offset = dictionary->words->offsets_by_length[len] + len * word_idx; |
156 | 0 | if (len > max_length) { |
157 | 0 | return BROTLI_FALSE; |
158 | 0 | } |
159 | | |
160 | 0 | matchlen = |
161 | 0 | FindMatchLengthWithLimit(data, &dictionary->words->data[offset], len); |
162 | 0 | if (matchlen + dictionary->cutoffTransformsCount <= len || matchlen == 0) { |
163 | 0 | return BROTLI_FALSE; |
164 | 0 | } |
165 | 0 | { |
166 | 0 | size_t cut = len - matchlen; |
167 | 0 | size_t transform_id = (cut << 2) + |
168 | 0 | (size_t)((dictionary->cutoffTransforms >> (cut * 6)) & 0x3F); |
169 | 0 | backward = max_backward + 1 + word_idx + |
170 | 0 | (transform_id << dictionary->words->size_bits_by_length[len]); |
171 | 0 | } |
172 | 0 | if (backward > max_distance) { |
173 | 0 | return BROTLI_FALSE; |
174 | 0 | } |
175 | 0 | score = BackwardReferenceScore(matchlen, backward); |
176 | 0 | if (score < out->score) { |
177 | 0 | return BROTLI_FALSE; |
178 | 0 | } |
179 | 0 | out->len = matchlen; |
180 | 0 | out->len_code_delta = (int)len - (int)matchlen; |
181 | 0 | out->distance = backward; |
182 | 0 | out->score = score; |
183 | 0 | return BROTLI_TRUE; |
184 | 0 | } Unexecuted instantiation: encode.c:TestStaticDictionaryItem Unexecuted instantiation: encoder_dict.c:TestStaticDictionaryItem Unexecuted instantiation: metablock.c:TestStaticDictionaryItem Unexecuted instantiation: backward_references.c:TestStaticDictionaryItem Unexecuted instantiation: backward_references_hq.c:TestStaticDictionaryItem Unexecuted instantiation: brotli_bit_stream.c:TestStaticDictionaryItem Unexecuted instantiation: compress_fragment.c:TestStaticDictionaryItem Unexecuted instantiation: compress_fragment_two_pass.c:TestStaticDictionaryItem |
185 | | |
186 | | static BROTLI_INLINE void SearchInStaticDictionary( |
187 | | const BrotliEncoderDictionary* dictionary, |
188 | | HasherCommon* common, const uint8_t* data, size_t max_length, |
189 | | size_t max_backward, size_t max_distance, |
190 | 0 | HasherSearchResult* out, BROTLI_BOOL shallow) { |
191 | 0 | size_t key; |
192 | 0 | size_t i; |
193 | 0 | if (common->dict_num_matches < (common->dict_num_lookups >> 7)) { |
194 | 0 | return; |
195 | 0 | } |
196 | 0 | key = Hash14(data) << 1; |
197 | 0 | for (i = 0; i < (shallow ? 1u : 2u); ++i, ++key) { |
198 | 0 | common->dict_num_lookups++; |
199 | 0 | if (dictionary->hash_table_lengths[key] != 0) { |
200 | 0 | BROTLI_BOOL item_matches = TestStaticDictionaryItem( |
201 | 0 | dictionary, dictionary->hash_table_lengths[key], |
202 | 0 | dictionary->hash_table_words[key], data, |
203 | 0 | max_length, max_backward, max_distance, out); |
204 | 0 | if (item_matches) { |
205 | 0 | common->dict_num_matches++; |
206 | 0 | } |
207 | 0 | } |
208 | 0 | } |
209 | 0 | } Unexecuted instantiation: encode.c:SearchInStaticDictionary Unexecuted instantiation: encoder_dict.c:SearchInStaticDictionary Unexecuted instantiation: metablock.c:SearchInStaticDictionary Unexecuted instantiation: backward_references.c:SearchInStaticDictionary Unexecuted instantiation: backward_references_hq.c:SearchInStaticDictionary Unexecuted instantiation: brotli_bit_stream.c:SearchInStaticDictionary Unexecuted instantiation: compress_fragment.c:SearchInStaticDictionary Unexecuted instantiation: compress_fragment_two_pass.c:SearchInStaticDictionary |
210 | | |
211 | | typedef struct BackwardMatch { |
212 | | uint32_t distance; |
213 | | uint32_t length_and_code; |
214 | | } BackwardMatch; |
215 | | |
216 | | static BROTLI_INLINE void InitBackwardMatch(BackwardMatch* self, |
217 | 0 | size_t dist, size_t len) { |
218 | 0 | self->distance = (uint32_t)dist; |
219 | 0 | self->length_and_code = (uint32_t)(len << 5); |
220 | 0 | } Unexecuted instantiation: encode.c:InitBackwardMatch Unexecuted instantiation: encoder_dict.c:InitBackwardMatch Unexecuted instantiation: metablock.c:InitBackwardMatch Unexecuted instantiation: backward_references.c:InitBackwardMatch Unexecuted instantiation: backward_references_hq.c:InitBackwardMatch Unexecuted instantiation: brotli_bit_stream.c:InitBackwardMatch Unexecuted instantiation: compress_fragment.c:InitBackwardMatch Unexecuted instantiation: compress_fragment_two_pass.c:InitBackwardMatch |
221 | | |
222 | | static BROTLI_INLINE void InitDictionaryBackwardMatch(BackwardMatch* self, |
223 | 0 | size_t dist, size_t len, size_t len_code) { |
224 | 0 | self->distance = (uint32_t)dist; |
225 | 0 | self->length_and_code = |
226 | 0 | (uint32_t)((len << 5) | (len == len_code ? 0 : len_code)); |
227 | 0 | } Unexecuted instantiation: encode.c:InitDictionaryBackwardMatch Unexecuted instantiation: encoder_dict.c:InitDictionaryBackwardMatch Unexecuted instantiation: metablock.c:InitDictionaryBackwardMatch Unexecuted instantiation: backward_references.c:InitDictionaryBackwardMatch Unexecuted instantiation: backward_references_hq.c:InitDictionaryBackwardMatch Unexecuted instantiation: brotli_bit_stream.c:InitDictionaryBackwardMatch Unexecuted instantiation: compress_fragment.c:InitDictionaryBackwardMatch Unexecuted instantiation: compress_fragment_two_pass.c:InitDictionaryBackwardMatch |
228 | | |
229 | 0 | static BROTLI_INLINE size_t BackwardMatchLength(const BackwardMatch* self) { |
230 | 0 | return self->length_and_code >> 5; |
231 | 0 | } Unexecuted instantiation: encode.c:BackwardMatchLength Unexecuted instantiation: encoder_dict.c:BackwardMatchLength Unexecuted instantiation: metablock.c:BackwardMatchLength Unexecuted instantiation: backward_references.c:BackwardMatchLength Unexecuted instantiation: backward_references_hq.c:BackwardMatchLength Unexecuted instantiation: brotli_bit_stream.c:BackwardMatchLength Unexecuted instantiation: compress_fragment.c:BackwardMatchLength Unexecuted instantiation: compress_fragment_two_pass.c:BackwardMatchLength |
232 | | |
233 | 0 | static BROTLI_INLINE size_t BackwardMatchLengthCode(const BackwardMatch* self) { |
234 | 0 | size_t code = self->length_and_code & 31; |
235 | 0 | return code ? code : BackwardMatchLength(self); |
236 | 0 | } Unexecuted instantiation: encode.c:BackwardMatchLengthCode Unexecuted instantiation: encoder_dict.c:BackwardMatchLengthCode Unexecuted instantiation: metablock.c:BackwardMatchLengthCode Unexecuted instantiation: backward_references.c:BackwardMatchLengthCode Unexecuted instantiation: backward_references_hq.c:BackwardMatchLengthCode Unexecuted instantiation: brotli_bit_stream.c:BackwardMatchLengthCode Unexecuted instantiation: compress_fragment.c:BackwardMatchLengthCode Unexecuted instantiation: compress_fragment_two_pass.c:BackwardMatchLengthCode |
237 | | |
238 | 0 | #define EXPAND_CAT(a, b) CAT(a, b) |
239 | 0 | #define CAT(a, b) a ## b |
240 | 0 | #define FN(X) EXPAND_CAT(X, HASHER()) |
241 | | |
242 | | #define HASHER() H10 |
243 | 0 | #define BUCKET_BITS 17 |
244 | 0 | #define MAX_TREE_SEARCH_DEPTH 64 |
245 | 0 | #define MAX_TREE_COMP_LENGTH 128 |
246 | | #include "hash_to_binary_tree_inc.h" /* NOLINT(build/include) */ |
247 | | #undef MAX_TREE_SEARCH_DEPTH |
248 | | #undef MAX_TREE_COMP_LENGTH |
249 | | #undef BUCKET_BITS |
250 | | #undef HASHER |
251 | | /* MAX_NUM_MATCHES == 64 + MAX_TREE_SEARCH_DEPTH */ |
252 | 0 | #define MAX_NUM_MATCHES_H10 128 |
253 | | |
254 | | /* For BUCKET_SWEEP_BITS == 0, enabling the dictionary lookup makes compression |
255 | | a little faster (0.5% - 1%) and it compresses 0.15% better on small text |
256 | | and HTML inputs. */ |
257 | | |
258 | | #define HASHER() H2 |
259 | 0 | #define BUCKET_BITS 16 |
260 | 0 | #define BUCKET_SWEEP_BITS 0 |
261 | 0 | #define HASH_LEN 5 |
262 | 0 | #define USE_DICTIONARY 1 |
263 | | #include "hash_longest_match_quickly_inc.h" /* NOLINT(build/include) */ |
264 | | #undef BUCKET_SWEEP_BITS |
265 | | #undef USE_DICTIONARY |
266 | | #undef HASHER |
267 | | |
268 | | #define HASHER() H3 |
269 | 0 | #define BUCKET_SWEEP_BITS 1 |
270 | 0 | #define USE_DICTIONARY 0 |
271 | | #include "hash_longest_match_quickly_inc.h" /* NOLINT(build/include) */ |
272 | | #undef USE_DICTIONARY |
273 | | #undef BUCKET_SWEEP_BITS |
274 | | #undef BUCKET_BITS |
275 | | #undef HASHER |
276 | | |
277 | | #define HASHER() H4 |
278 | 0 | #define BUCKET_BITS 17 |
279 | 0 | #define BUCKET_SWEEP_BITS 2 |
280 | 0 | #define USE_DICTIONARY 1 |
281 | | #include "hash_longest_match_quickly_inc.h" /* NOLINT(build/include) */ |
282 | | #undef USE_DICTIONARY |
283 | | #undef HASH_LEN |
284 | | #undef BUCKET_SWEEP_BITS |
285 | | #undef BUCKET_BITS |
286 | | #undef HASHER |
287 | | |
288 | | #define HASHER() H5 |
289 | | #include "hash_longest_match_inc.h" /* NOLINT(build/include) */ |
290 | | #undef HASHER |
291 | | |
292 | | #define HASHER() H6 |
293 | | #include "hash_longest_match64_inc.h" /* NOLINT(build/include) */ |
294 | | #undef HASHER |
295 | | |
296 | | #if defined(BROTLI_MAX_SIMD_QUALITY) |
297 | | #define HASHER() H58 |
298 | | #include "hash_longest_match_simd_inc.h" /* NOLINT(build/include) */ |
299 | | #undef HASHER |
300 | | |
301 | | #define HASHER() H59 |
302 | | #include "hash_longest_match_simd_opt_inc.h" /* NOLINT(build/include) */ |
303 | | #undef HASHER |
304 | | |
305 | | #define HASHER() H68 |
306 | | #include "hash_longest_match64_simd_inc.h" /* NOLINT(build/include) */ |
307 | | #undef HASHER |
308 | | #endif |
309 | | |
310 | 0 | #define BUCKET_BITS 15 |
311 | | |
312 | 0 | #define NUM_LAST_DISTANCES_TO_CHECK 4 |
313 | 0 | #define NUM_BANKS 1 |
314 | 0 | #define BANK_BITS 16 |
315 | | #define HASHER() H40 |
316 | | #include "hash_forgetful_chain_inc.h" /* NOLINT(build/include) */ |
317 | | #undef HASHER |
318 | | #undef NUM_LAST_DISTANCES_TO_CHECK |
319 | | |
320 | 0 | #define NUM_LAST_DISTANCES_TO_CHECK 10 |
321 | | #define HASHER() H41 |
322 | | #include "hash_forgetful_chain_inc.h" /* NOLINT(build/include) */ |
323 | | #undef HASHER |
324 | | #undef NUM_LAST_DISTANCES_TO_CHECK |
325 | | #undef NUM_BANKS |
326 | | #undef BANK_BITS |
327 | | |
328 | 0 | #define NUM_LAST_DISTANCES_TO_CHECK 16 |
329 | 0 | #define NUM_BANKS 512 |
330 | 0 | #define BANK_BITS 9 |
331 | | #define HASHER() H42 |
332 | | #include "hash_forgetful_chain_inc.h" /* NOLINT(build/include) */ |
333 | | #undef HASHER |
334 | | #undef NUM_LAST_DISTANCES_TO_CHECK |
335 | | #undef NUM_BANKS |
336 | | #undef BANK_BITS |
337 | | |
338 | | #undef BUCKET_BITS |
339 | | |
340 | | #define HASHER() H54 |
341 | 0 | #define BUCKET_BITS 20 |
342 | 0 | #define BUCKET_SWEEP_BITS 2 |
343 | 0 | #define HASH_LEN 7 |
344 | 0 | #define USE_DICTIONARY 0 |
345 | | #include "hash_longest_match_quickly_inc.h" /* NOLINT(build/include) */ |
346 | | #undef USE_DICTIONARY |
347 | | #undef HASH_LEN |
348 | | #undef BUCKET_SWEEP_BITS |
349 | | #undef BUCKET_BITS |
350 | | #undef HASHER |
351 | | |
352 | | /* fast large window hashers */ |
353 | | |
354 | | #define HASHER() HROLLING_FAST |
355 | 0 | #define CHUNKLEN 32 |
356 | 0 | #define JUMP 4 |
357 | 0 | #define NUMBUCKETS 16777216 |
358 | 0 | #define MASK ((NUMBUCKETS * 64) - 1) |
359 | | #include "hash_rolling_inc.h" /* NOLINT(build/include) */ |
360 | | #undef JUMP |
361 | | #undef HASHER |
362 | | |
363 | | |
364 | | #define HASHER() HROLLING |
365 | 0 | #define JUMP 1 |
366 | | #include "hash_rolling_inc.h" /* NOLINT(build/include) */ |
367 | | #undef MASK |
368 | | #undef NUMBUCKETS |
369 | | #undef JUMP |
370 | | #undef CHUNKLEN |
371 | | #undef HASHER |
372 | | |
373 | | #define HASHER() H35 |
374 | | #define HASHER_A H3 |
375 | | #define HASHER_B HROLLING_FAST |
376 | | #include "hash_composite_inc.h" /* NOLINT(build/include) */ |
377 | | #undef HASHER_A |
378 | | #undef HASHER_B |
379 | | #undef HASHER |
380 | | |
381 | | #define HASHER() H55 |
382 | | #define HASHER_A H54 |
383 | | #define HASHER_B HROLLING_FAST |
384 | | #include "hash_composite_inc.h" /* NOLINT(build/include) */ |
385 | | #undef HASHER_A |
386 | | #undef HASHER_B |
387 | | #undef HASHER |
388 | | |
389 | | #define HASHER() H65 |
390 | | #define HASHER_A H6 |
391 | | #define HASHER_B HROLLING |
392 | | #include "hash_composite_inc.h" /* NOLINT(build/include) */ |
393 | | #undef HASHER_A |
394 | | #undef HASHER_B |
395 | | #undef HASHER |
396 | | |
397 | | #undef FN |
398 | | #undef CAT |
399 | | #undef EXPAND_CAT |
400 | | |
401 | | #if defined(BROTLI_MAX_SIMD_QUALITY) |
402 | | #define FOR_SIMPLE_HASHERS(H) \ |
403 | 0 | H(2) H(3) H(4) H(5) H(6) H(40) H(41) H(42) H(54) H(58) H(59) H(68) |
404 | | #else |
405 | | #define FOR_SIMPLE_HASHERS(H) \ |
406 | | H(2) H(3) H(4) H(5) H(6) H(40) H(41) H(42) H(54) |
407 | | #endif |
408 | 0 | #define FOR_COMPOSITE_HASHERS(H) H(35) H(55) H(65) |
409 | 0 | #define FOR_GENERIC_HASHERS(H) FOR_SIMPLE_HASHERS(H) FOR_COMPOSITE_HASHERS(H) |
410 | 0 | #define FOR_ALL_HASHERS(H) FOR_GENERIC_HASHERS(H) H(10) |
411 | | |
412 | | typedef struct { |
413 | | HasherCommon common; |
414 | | |
415 | | union { |
416 | | #define MEMBER_(N) \ |
417 | | H ## N _H ## N; |
418 | | FOR_ALL_HASHERS(MEMBER_) |
419 | | #undef MEMBER_ |
420 | | } privat; |
421 | | } Hasher; |
422 | | |
423 | | /* MUST be invoked before any other method. */ |
424 | 0 | static BROTLI_INLINE void HasherInit(Hasher* hasher) { |
425 | 0 | hasher->common.is_setup_ = BROTLI_FALSE; |
426 | 0 | hasher->common.extra[0] = NULL; |
427 | 0 | hasher->common.extra[1] = NULL; |
428 | 0 | hasher->common.extra[2] = NULL; |
429 | 0 | hasher->common.extra[3] = NULL; |
430 | 0 | hasher->common.base64_regions = NULL; |
431 | 0 | } Unexecuted instantiation: encode.c:HasherInit Unexecuted instantiation: encoder_dict.c:HasherInit Unexecuted instantiation: metablock.c:HasherInit Unexecuted instantiation: backward_references.c:HasherInit Unexecuted instantiation: backward_references_hq.c:HasherInit Unexecuted instantiation: brotli_bit_stream.c:HasherInit Unexecuted instantiation: compress_fragment.c:HasherInit Unexecuted instantiation: compress_fragment_two_pass.c:HasherInit |
432 | | |
433 | 0 | static BROTLI_INLINE void DestroyHasher(MemoryManager* m, Hasher* hasher) { |
434 | 0 | if (hasher->common.extra[0] != NULL) BROTLI_FREE(m, hasher->common.extra[0]); |
435 | 0 | if (hasher->common.extra[1] != NULL) BROTLI_FREE(m, hasher->common.extra[1]); |
436 | 0 | if (hasher->common.extra[2] != NULL) BROTLI_FREE(m, hasher->common.extra[2]); |
437 | 0 | if (hasher->common.extra[3] != NULL) BROTLI_FREE(m, hasher->common.extra[3]); |
438 | 0 | if (hasher->common.base64_regions != NULL) { |
439 | 0 | BROTLI_FREE(m, hasher->common.base64_regions); |
440 | 0 | } |
441 | 0 | } Unexecuted instantiation: encode.c:DestroyHasher Unexecuted instantiation: encoder_dict.c:DestroyHasher Unexecuted instantiation: metablock.c:DestroyHasher Unexecuted instantiation: backward_references.c:DestroyHasher Unexecuted instantiation: backward_references_hq.c:DestroyHasher Unexecuted instantiation: brotli_bit_stream.c:DestroyHasher Unexecuted instantiation: compress_fragment.c:DestroyHasher Unexecuted instantiation: compress_fragment_two_pass.c:DestroyHasher |
442 | | |
443 | 0 | static BROTLI_INLINE void HasherReset(Hasher* hasher) { |
444 | 0 | hasher->common.is_prepared_ = BROTLI_FALSE; |
445 | 0 | } Unexecuted instantiation: encode.c:HasherReset Unexecuted instantiation: encoder_dict.c:HasherReset Unexecuted instantiation: metablock.c:HasherReset Unexecuted instantiation: backward_references.c:HasherReset Unexecuted instantiation: backward_references_hq.c:HasherReset Unexecuted instantiation: brotli_bit_stream.c:HasherReset Unexecuted instantiation: compress_fragment.c:HasherReset Unexecuted instantiation: compress_fragment_two_pass.c:HasherReset |
446 | | |
447 | | static BROTLI_INLINE void HasherSize(const BrotliEncoderParams* params, |
448 | 0 | BROTLI_BOOL one_shot, const size_t input_size, size_t* alloc_size) { |
449 | 0 | switch (params->hasher.type) { |
450 | 0 | #define SIZE_(N) \ |
451 | 0 | case N: \ |
452 | 0 | HashMemAllocInBytesH ## N(params, one_shot, input_size, alloc_size); \ |
453 | 0 | break; |
454 | 0 | FOR_ALL_HASHERS(SIZE_) |
455 | 0 | #undef SIZE_ |
456 | 0 | default: |
457 | 0 | break; |
458 | 0 | } |
459 | 0 | } Unexecuted instantiation: encode.c:HasherSize Unexecuted instantiation: encoder_dict.c:HasherSize Unexecuted instantiation: metablock.c:HasherSize Unexecuted instantiation: backward_references.c:HasherSize Unexecuted instantiation: backward_references_hq.c:HasherSize Unexecuted instantiation: brotli_bit_stream.c:HasherSize Unexecuted instantiation: compress_fragment.c:HasherSize Unexecuted instantiation: compress_fragment_two_pass.c:HasherSize |
460 | | |
461 | | static BROTLI_INLINE void HasherSetup(MemoryManager* m, Hasher* hasher, |
462 | | BrotliEncoderParams* params, const uint8_t* data, size_t position, |
463 | 0 | size_t input_size, BROTLI_BOOL is_last) { |
464 | 0 | BROTLI_BOOL one_shot = (position == 0 && is_last); |
465 | 0 | if (!hasher->common.is_setup_) { |
466 | 0 | size_t alloc_size[4] = {0}; |
467 | 0 | size_t i; |
468 | 0 | ChooseHasher(params, ¶ms->hasher); |
469 | 0 | hasher->common.params = params->hasher; |
470 | 0 | hasher->common.dict_num_lookups = 0; |
471 | 0 | hasher->common.dict_num_matches = 0; |
472 | 0 | HasherSize(params, one_shot, input_size, alloc_size); |
473 | 0 | for (i = 0; i < 4; ++i) { |
474 | 0 | if (alloc_size[i] == 0) continue; |
475 | 0 | hasher->common.extra[i] = BROTLI_ALLOC(m, uint8_t, alloc_size[i]); |
476 | 0 | if (BROTLI_IS_OOM(m) || BROTLI_IS_NULL(hasher->common.extra[i])) return; |
477 | 0 | } |
478 | 0 | if (params->base64_mode && params->max_base64_regions > 0) { |
479 | 0 | hasher->common.base64_regions = BROTLI_ALLOC( |
480 | 0 | m, Base64Region, params->max_base64_regions); |
481 | 0 | if (BROTLI_IS_OOM(m) || BROTLI_IS_NULL(hasher->common.base64_regions)) { |
482 | 0 | return; |
483 | 0 | } |
484 | 0 | } |
485 | 0 | switch (hasher->common.params.type) { |
486 | 0 | #define INITIALIZE_(N) \ |
487 | 0 | case N: \ |
488 | 0 | InitializeH ## N(&hasher->common, \ |
489 | 0 | &hasher->privat._H ## N, params); \ |
490 | 0 | break; |
491 | 0 | FOR_ALL_HASHERS(INITIALIZE_); |
492 | 0 | #undef INITIALIZE_ |
493 | 0 | default: |
494 | 0 | break; |
495 | 0 | } |
496 | 0 | HasherReset(hasher); |
497 | 0 | hasher->common.is_setup_ = BROTLI_TRUE; |
498 | 0 | } |
499 | | |
500 | 0 | if (!hasher->common.is_prepared_) { |
501 | 0 | switch (hasher->common.params.type) { |
502 | 0 | #define PREPARE_(N) \ |
503 | 0 | case N: \ |
504 | 0 | PrepareH ## N( \ |
505 | 0 | &hasher->privat._H ## N, \ |
506 | 0 | one_shot, input_size, data); \ |
507 | 0 | break; |
508 | 0 | FOR_ALL_HASHERS(PREPARE_) |
509 | 0 | #undef PREPARE_ |
510 | 0 | default: break; |
511 | 0 | } |
512 | 0 | hasher->common.is_prepared_ = BROTLI_TRUE; |
513 | 0 | } |
514 | 0 | } Unexecuted instantiation: encode.c:HasherSetup Unexecuted instantiation: encoder_dict.c:HasherSetup Unexecuted instantiation: metablock.c:HasherSetup Unexecuted instantiation: backward_references.c:HasherSetup Unexecuted instantiation: backward_references_hq.c:HasherSetup Unexecuted instantiation: brotli_bit_stream.c:HasherSetup Unexecuted instantiation: compress_fragment.c:HasherSetup Unexecuted instantiation: compress_fragment_two_pass.c:HasherSetup |
515 | | |
516 | | static BROTLI_INLINE void InitOrStitchToPreviousBlock( |
517 | | MemoryManager* m, Hasher* hasher, const uint8_t* data, size_t mask, |
518 | | BrotliEncoderParams* params, size_t position, size_t input_size, |
519 | 0 | BROTLI_BOOL is_last) { |
520 | 0 | HasherSetup(m, hasher, params, data, position, input_size, is_last); |
521 | 0 | if (BROTLI_IS_OOM(m)) return; |
522 | 0 | switch (hasher->common.params.type) { |
523 | 0 | #define INIT_(N) \ |
524 | 0 | case N: \ |
525 | 0 | StitchToPreviousBlockH ## N( \ |
526 | 0 | &hasher->privat._H ## N, \ |
527 | 0 | input_size, position, data, mask); \ |
528 | 0 | break; |
529 | 0 | FOR_ALL_HASHERS(INIT_) |
530 | 0 | #undef INIT_ |
531 | 0 | default: break; |
532 | 0 | } |
533 | 0 | } Unexecuted instantiation: encode.c:InitOrStitchToPreviousBlock Unexecuted instantiation: encoder_dict.c:InitOrStitchToPreviousBlock Unexecuted instantiation: metablock.c:InitOrStitchToPreviousBlock Unexecuted instantiation: backward_references.c:InitOrStitchToPreviousBlock Unexecuted instantiation: backward_references_hq.c:InitOrStitchToPreviousBlock Unexecuted instantiation: brotli_bit_stream.c:InitOrStitchToPreviousBlock Unexecuted instantiation: compress_fragment.c:InitOrStitchToPreviousBlock Unexecuted instantiation: compress_fragment_two_pass.c:InitOrStitchToPreviousBlock |
534 | | |
535 | | /* NB: when seamless dictionary-ring-buffer copies are implemented, don't forget |
536 | | to add proper guards for non-zero-BROTLI_PARAM_STREAM_OFFSET. */ |
537 | | static BROTLI_INLINE void FindCompoundDictionaryMatch( |
538 | | const PreparedDictionary* self, const uint8_t* BROTLI_RESTRICT data, |
539 | | const size_t ring_buffer_mask, const int* BROTLI_RESTRICT distance_cache, |
540 | | const size_t cur_ix, const size_t max_length, const size_t distance_offset, |
541 | 0 | const size_t max_distance, HasherSearchResult* BROTLI_RESTRICT out) { |
542 | 0 | const uint32_t source_size = self->source_size; |
543 | 0 | const size_t boundary = distance_offset - source_size; |
544 | 0 | const uint32_t hash_bits = self->hash_bits; |
545 | 0 | const uint32_t bucket_bits = self->bucket_bits; |
546 | 0 | const uint32_t slot_bits = self->slot_bits; |
547 | |
|
548 | 0 | const uint32_t hash_shift = 64u - bucket_bits; |
549 | 0 | const uint32_t slot_mask = (~((uint32_t)0U)) >> (32 - slot_bits); |
550 | 0 | const uint64_t hash_mask = (~((uint64_t)0U)) >> (64 - hash_bits); |
551 | |
|
552 | 0 | const uint32_t* slot_offsets = (uint32_t*)(&self[1]); |
553 | 0 | const uint16_t* heads = (uint16_t*)(&slot_offsets[(size_t)1u << slot_bits]); |
554 | 0 | const uint32_t* items = (uint32_t*)(&heads[(size_t)1u << bucket_bits]); |
555 | 0 | const uint8_t* source = NULL; |
556 | |
|
557 | 0 | const size_t cur_ix_masked = cur_ix & ring_buffer_mask; |
558 | 0 | score_t best_score = out->score; |
559 | 0 | size_t best_len = out->len; |
560 | 0 | size_t i; |
561 | 0 | const uint64_t h = |
562 | 0 | (BROTLI_UNALIGNED_LOAD64LE(&data[cur_ix_masked]) & hash_mask) * |
563 | 0 | kPreparedDictionaryHashMul64Long; |
564 | 0 | const uint32_t key = (uint32_t)(h >> hash_shift); |
565 | 0 | const uint32_t slot = key & slot_mask; |
566 | 0 | const uint32_t head = heads[key]; |
567 | 0 | const uint32_t* BROTLI_RESTRICT chain = &items[slot_offsets[slot] + head]; |
568 | 0 | uint32_t item = (head == 0xFFFF) ? 1 : 0; |
569 | |
|
570 | 0 | const void* tail = (void*)&items[self->num_items]; |
571 | 0 | if (self->magic == kPreparedDictionaryMagic) { |
572 | 0 | source = (const uint8_t*)tail; |
573 | 0 | } else { |
574 | | /* kLeanPreparedDictionaryMagic */ |
575 | 0 | source = (const uint8_t*)BROTLI_UNALIGNED_LOAD_PTR((const uint8_t**)tail); |
576 | 0 | } |
577 | |
|
578 | 0 | for (i = 0; i < 4; ++i) { |
579 | 0 | const size_t distance = (size_t)distance_cache[i]; |
580 | 0 | size_t offset; |
581 | 0 | size_t limit; |
582 | 0 | size_t len; |
583 | 0 | if (distance <= boundary || distance > distance_offset) continue; |
584 | 0 | offset = distance_offset - distance; |
585 | 0 | limit = source_size - offset; |
586 | 0 | limit = limit > max_length ? max_length : limit; |
587 | 0 | len = FindMatchLengthWithLimit(&source[offset], &data[cur_ix_masked], |
588 | 0 | limit); |
589 | 0 | if (len >= 2) { |
590 | 0 | score_t score = BackwardReferenceScoreUsingLastDistance(len); |
591 | 0 | if (best_score < score) { |
592 | 0 | if (i != 0) score -= BackwardReferencePenaltyUsingLastDistance(i); |
593 | 0 | if (best_score < score) { |
594 | 0 | best_score = score; |
595 | 0 | if (len > best_len) best_len = len; |
596 | 0 | out->len = len; |
597 | 0 | out->len_code_delta = 0; |
598 | 0 | out->distance = distance; |
599 | 0 | out->score = best_score; |
600 | 0 | } |
601 | 0 | } |
602 | 0 | } |
603 | 0 | } |
604 | | /* we require matches of len >4, so increase best_len to 3, so we can compare |
605 | | * 4 bytes all the time. */ |
606 | 0 | if (best_len < 3) { |
607 | 0 | best_len = 3; |
608 | 0 | } |
609 | 0 | while (item == 0) { |
610 | 0 | size_t offset; |
611 | 0 | size_t distance; |
612 | 0 | size_t limit; |
613 | 0 | item = *chain; |
614 | 0 | chain++; |
615 | 0 | offset = item & 0x7FFFFFFF; |
616 | 0 | item &= 0x80000000; |
617 | 0 | distance = distance_offset - offset; |
618 | 0 | limit = source_size - offset; |
619 | 0 | limit = (limit > max_length) ? max_length : limit; |
620 | 0 | if (distance > max_distance) continue; |
621 | 0 | if (cur_ix_masked + best_len > ring_buffer_mask || best_len >= limit || |
622 | | /* compare 4 bytes ending at best_len + 1 */ |
623 | 0 | BrotliUnalignedRead32(&data[cur_ix_masked + best_len - 3]) != |
624 | 0 | BrotliUnalignedRead32(&source[offset + best_len - 3])) { |
625 | 0 | continue; |
626 | 0 | } |
627 | 0 | { |
628 | 0 | const size_t len = FindMatchLengthWithLimit(&source[offset], |
629 | 0 | &data[cur_ix_masked], |
630 | 0 | limit); |
631 | 0 | if (len >= 4) { |
632 | 0 | score_t score = BackwardReferenceScore(len, distance); |
633 | 0 | if (best_score < score) { |
634 | 0 | best_score = score; |
635 | 0 | best_len = len; |
636 | 0 | out->len = best_len; |
637 | 0 | out->len_code_delta = 0; |
638 | 0 | out->distance = distance; |
639 | 0 | out->score = best_score; |
640 | 0 | } |
641 | 0 | } |
642 | 0 | } |
643 | 0 | } |
644 | 0 | } Unexecuted instantiation: encode.c:FindCompoundDictionaryMatch Unexecuted instantiation: encoder_dict.c:FindCompoundDictionaryMatch Unexecuted instantiation: metablock.c:FindCompoundDictionaryMatch Unexecuted instantiation: backward_references.c:FindCompoundDictionaryMatch Unexecuted instantiation: backward_references_hq.c:FindCompoundDictionaryMatch Unexecuted instantiation: brotli_bit_stream.c:FindCompoundDictionaryMatch Unexecuted instantiation: compress_fragment.c:FindCompoundDictionaryMatch Unexecuted instantiation: compress_fragment_two_pass.c:FindCompoundDictionaryMatch |
645 | | |
646 | | /* NB: when seamless dictionary-ring-buffer copies are implemented, don't forget |
647 | | to add proper guards for non-zero-BROTLI_PARAM_STREAM_OFFSET. */ |
648 | | static BROTLI_INLINE size_t FindAllCompoundDictionaryMatches( |
649 | | const PreparedDictionary* self, const uint8_t* BROTLI_RESTRICT data, |
650 | | const size_t ring_buffer_mask, const size_t cur_ix, const size_t min_length, |
651 | | const size_t max_length, const size_t distance_offset, |
652 | 0 | const size_t max_distance, BackwardMatch* matches, size_t match_limit) { |
653 | 0 | const uint32_t source_size = self->source_size; |
654 | 0 | const uint32_t hash_bits = self->hash_bits; |
655 | 0 | const uint32_t bucket_bits = self->bucket_bits; |
656 | 0 | const uint32_t slot_bits = self->slot_bits; |
657 | |
|
658 | 0 | const uint32_t hash_shift = 64u - bucket_bits; |
659 | 0 | const uint32_t slot_mask = (~((uint32_t)0U)) >> (32 - slot_bits); |
660 | 0 | const uint64_t hash_mask = (~((uint64_t)0U)) >> (64 - hash_bits); |
661 | |
|
662 | 0 | const uint32_t* slot_offsets = (uint32_t*)(&self[1]); |
663 | 0 | const uint16_t* heads = (uint16_t*)(&slot_offsets[(size_t)1u << slot_bits]); |
664 | 0 | const uint32_t* items = (uint32_t*)(&heads[(size_t)1u << bucket_bits]); |
665 | 0 | const uint8_t* source = NULL; |
666 | |
|
667 | 0 | const size_t cur_ix_masked = cur_ix & ring_buffer_mask; |
668 | 0 | size_t best_len = min_length; |
669 | 0 | const uint64_t h = |
670 | 0 | (BROTLI_UNALIGNED_LOAD64LE(&data[cur_ix_masked]) & hash_mask) * |
671 | 0 | kPreparedDictionaryHashMul64Long; |
672 | 0 | const uint32_t key = (uint32_t)(h >> hash_shift); |
673 | 0 | const uint32_t slot = key & slot_mask; |
674 | 0 | const uint32_t head = heads[key]; |
675 | 0 | const uint32_t* BROTLI_RESTRICT chain = &items[slot_offsets[slot] + head]; |
676 | 0 | uint32_t item = (head == 0xFFFF) ? 1 : 0; |
677 | 0 | size_t found = 0; |
678 | |
|
679 | 0 | const void* tail = (void*)&items[self->num_items]; |
680 | 0 | if (self->magic == kPreparedDictionaryMagic) { |
681 | 0 | source = (const uint8_t*)tail; |
682 | 0 | } else { |
683 | | /* kLeanPreparedDictionaryMagic */ |
684 | 0 | source = (const uint8_t*)BROTLI_UNALIGNED_LOAD_PTR((const uint8_t**)tail); |
685 | 0 | } |
686 | |
|
687 | 0 | while (item == 0) { |
688 | 0 | size_t offset; |
689 | 0 | size_t distance; |
690 | 0 | size_t limit; |
691 | 0 | size_t len; |
692 | 0 | item = *chain; |
693 | 0 | chain++; |
694 | 0 | offset = item & 0x7FFFFFFF; |
695 | 0 | item &= 0x80000000; |
696 | 0 | distance = distance_offset - offset; |
697 | 0 | limit = source_size - offset; |
698 | 0 | limit = (limit > max_length) ? max_length : limit; |
699 | 0 | if (distance > max_distance) continue; |
700 | 0 | if (cur_ix_masked + best_len > ring_buffer_mask || |
701 | 0 | best_len >= limit || |
702 | 0 | data[cur_ix_masked + best_len] != source[offset + best_len]) { |
703 | 0 | continue; |
704 | 0 | } |
705 | 0 | len = FindMatchLengthWithLimit( |
706 | 0 | &source[offset], &data[cur_ix_masked], limit); |
707 | 0 | if (len > best_len) { |
708 | 0 | best_len = len; |
709 | 0 | InitBackwardMatch(matches++, distance, len); |
710 | 0 | found++; |
711 | 0 | if (found == match_limit) break; |
712 | 0 | } |
713 | 0 | } |
714 | 0 | return found; |
715 | 0 | } Unexecuted instantiation: encode.c:FindAllCompoundDictionaryMatches Unexecuted instantiation: encoder_dict.c:FindAllCompoundDictionaryMatches Unexecuted instantiation: metablock.c:FindAllCompoundDictionaryMatches Unexecuted instantiation: backward_references.c:FindAllCompoundDictionaryMatches Unexecuted instantiation: backward_references_hq.c:FindAllCompoundDictionaryMatches Unexecuted instantiation: brotli_bit_stream.c:FindAllCompoundDictionaryMatches Unexecuted instantiation: compress_fragment.c:FindAllCompoundDictionaryMatches Unexecuted instantiation: compress_fragment_two_pass.c:FindAllCompoundDictionaryMatches |
716 | | |
717 | | static BROTLI_INLINE void LookupCompoundDictionaryMatch( |
718 | | const CompoundDictionary* addon, const uint8_t* BROTLI_RESTRICT data, |
719 | | const size_t ring_buffer_mask, const int* BROTLI_RESTRICT distance_cache, |
720 | | const size_t cur_ix, const size_t max_length, |
721 | | const size_t max_ring_buffer_distance, const size_t max_distance, |
722 | 0 | HasherSearchResult* sr) { |
723 | 0 | size_t base_offset = max_ring_buffer_distance + 1 + addon->total_size - 1; |
724 | 0 | size_t d; |
725 | 0 | for (d = 0; d < addon->num_chunks; ++d) { |
726 | | /* Only one prepared dictionary type is currently supported. */ |
727 | 0 | FindCompoundDictionaryMatch( |
728 | 0 | (const PreparedDictionary*)addon->chunks[d], data, ring_buffer_mask, |
729 | 0 | distance_cache, cur_ix, max_length, |
730 | 0 | base_offset - addon->chunk_offsets[d], max_distance, sr); |
731 | 0 | } |
732 | 0 | } Unexecuted instantiation: encode.c:LookupCompoundDictionaryMatch Unexecuted instantiation: encoder_dict.c:LookupCompoundDictionaryMatch Unexecuted instantiation: metablock.c:LookupCompoundDictionaryMatch Unexecuted instantiation: backward_references.c:LookupCompoundDictionaryMatch Unexecuted instantiation: backward_references_hq.c:LookupCompoundDictionaryMatch Unexecuted instantiation: brotli_bit_stream.c:LookupCompoundDictionaryMatch Unexecuted instantiation: compress_fragment.c:LookupCompoundDictionaryMatch Unexecuted instantiation: compress_fragment_two_pass.c:LookupCompoundDictionaryMatch |
733 | | |
734 | | static BROTLI_INLINE size_t LookupAllCompoundDictionaryMatches( |
735 | | const CompoundDictionary* addon, const uint8_t* BROTLI_RESTRICT data, |
736 | | const size_t ring_buffer_mask, const size_t cur_ix, size_t min_length, |
737 | | const size_t max_length, const size_t max_ring_buffer_distance, |
738 | | const size_t max_distance, BackwardMatch* matches, |
739 | 0 | size_t match_limit) { |
740 | 0 | size_t base_offset = max_ring_buffer_distance + 1 + addon->total_size - 1; |
741 | 0 | size_t d; |
742 | 0 | size_t total_found = 0; |
743 | 0 | for (d = 0; d < addon->num_chunks; ++d) { |
744 | | /* Only one prepared dictionary type is currently supported. */ |
745 | 0 | total_found += FindAllCompoundDictionaryMatches( |
746 | 0 | (const PreparedDictionary*)addon->chunks[d], data, ring_buffer_mask, |
747 | 0 | cur_ix, min_length, max_length, base_offset - addon->chunk_offsets[d], |
748 | 0 | max_distance, matches + total_found, match_limit - total_found); |
749 | 0 | if (total_found == match_limit) break; |
750 | 0 | if (total_found > 0) { |
751 | 0 | min_length = BackwardMatchLength(&matches[total_found - 1]); |
752 | 0 | } |
753 | 0 | } |
754 | 0 | return total_found; |
755 | 0 | } Unexecuted instantiation: encode.c:LookupAllCompoundDictionaryMatches Unexecuted instantiation: encoder_dict.c:LookupAllCompoundDictionaryMatches Unexecuted instantiation: metablock.c:LookupAllCompoundDictionaryMatches Unexecuted instantiation: backward_references.c:LookupAllCompoundDictionaryMatches Unexecuted instantiation: backward_references_hq.c:LookupAllCompoundDictionaryMatches Unexecuted instantiation: brotli_bit_stream.c:LookupAllCompoundDictionaryMatches Unexecuted instantiation: compress_fragment.c:LookupAllCompoundDictionaryMatches Unexecuted instantiation: compress_fragment_two_pass.c:LookupAllCompoundDictionaryMatches |
756 | | |
757 | | /* Struct for handing state between the prefetch and find calls. Avoids |
758 | | * recalculating the hash and dependent variables. */ |
759 | | typedef struct PreparedDictionaryProbe { |
760 | | const uint32_t* chain; |
761 | | uint32_t item; |
762 | | } PreparedDictionaryProbe; |
763 | | |
764 | | static BROTLI_INLINE void PrefetchCompoundDictionaryMatchOpt( |
765 | | const CompoundDictionary* addon, const uint8_t* BROTLI_RESTRICT data, |
766 | | const size_t ring_buffer_mask, const size_t cur_ix, |
767 | 0 | PreparedDictionaryProbe* BROTLI_RESTRICT probes) { |
768 | 0 | const size_t cur_ix_masked = cur_ix & ring_buffer_mask; |
769 | 0 | const uint64_t bytes = BROTLI_UNALIGNED_LOAD64LE(&data[cur_ix_masked]); |
770 | 0 | size_t d; |
771 | 0 | for (d = 0; d < addon->num_chunks; ++d) { |
772 | 0 | const PreparedDictionaryView* view = &addon->chunk_views[d]; |
773 | 0 | const uint64_t h = |
774 | 0 | (bytes & view->hash_mask) * kPreparedDictionaryHashMul64Long; |
775 | 0 | const uint32_t key = (uint32_t)(h >> view->hash_shift); |
776 | 0 | const uint32_t slot = key & view->slot_mask; |
777 | 0 | const uint32_t head = view->heads[key]; |
778 | | /* Deliberately branchless - if head == 0xFFFF (no items), we'll prefetch |
779 | | * some garbage address. Prefetch can't fault, so this is safe.*/ |
780 | 0 | const uint32_t* chain = &view->items[view->slot_offsets[slot] + head]; |
781 | 0 | PREFETCH_L1(chain); |
782 | 0 | probes[d].chain = chain; |
783 | 0 | probes[d].item = (head == 0xFFFF) ? 1 : 0; |
784 | | |
785 | | /* One-ahead for the heads[] line of the next probe: the next search is |
786 | | at cur_ix + 1 (the lazy search, or the next position after a miss), |
787 | | whose hash covers bytes cur_ix+1.. -- the same 8-byte load shifted by |
788 | | one byte (hash_mask spans at most 56 bits for this to be exact; a |
789 | | mismatch would only waste the prefetch). The heads[key] load above |
790 | | misses L1 ~94% of the time (mostly L3 fills). */ |
791 | 0 | { |
792 | 0 | const uint64_t h1 = |
793 | 0 | ((bytes >> 8) & view->hash_mask) * kPreparedDictionaryHashMul64Long; |
794 | 0 | PREFETCH_L1(&view->heads[(uint32_t)(h1 >> view->hash_shift)]); |
795 | 0 | } |
796 | 0 | } |
797 | 0 | } Unexecuted instantiation: encode.c:PrefetchCompoundDictionaryMatchOpt Unexecuted instantiation: encoder_dict.c:PrefetchCompoundDictionaryMatchOpt Unexecuted instantiation: metablock.c:PrefetchCompoundDictionaryMatchOpt Unexecuted instantiation: backward_references.c:PrefetchCompoundDictionaryMatchOpt Unexecuted instantiation: backward_references_hq.c:PrefetchCompoundDictionaryMatchOpt Unexecuted instantiation: brotli_bit_stream.c:PrefetchCompoundDictionaryMatchOpt Unexecuted instantiation: compress_fragment.c:PrefetchCompoundDictionaryMatchOpt Unexecuted instantiation: compress_fragment_two_pass.c:PrefetchCompoundDictionaryMatchOpt |
798 | | |
799 | | /* The heads[] half of PrefetchCompoundDictionaryMatchOpt: hash the position |
800 | | and prefetch its heads[] line, with no dependent items[] load. Used at the |
801 | | commit site, where the successor position is known but the probe state for |
802 | | it would not survive the intervening StoreRange. */ |
803 | | static BROTLI_INLINE void PrefetchCompoundDictionaryHeadsOpt( |
804 | | const CompoundDictionary* addon, const uint8_t* BROTLI_RESTRICT data, |
805 | 0 | const size_t ring_buffer_mask, const size_t cur_ix) { |
806 | 0 | const uint64_t bytes = |
807 | 0 | BROTLI_UNALIGNED_LOAD64LE(&data[cur_ix & ring_buffer_mask]); |
808 | 0 | size_t d; |
809 | 0 | for (d = 0; d < addon->num_chunks; ++d) { |
810 | 0 | const PreparedDictionaryView* view = &addon->chunk_views[d]; |
811 | 0 | const uint64_t h = |
812 | 0 | (bytes & view->hash_mask) * kPreparedDictionaryHashMul64Long; |
813 | 0 | PREFETCH_L1(&view->heads[(uint32_t)(h >> view->hash_shift)]); |
814 | 0 | } |
815 | 0 | } Unexecuted instantiation: encode.c:PrefetchCompoundDictionaryHeadsOpt Unexecuted instantiation: encoder_dict.c:PrefetchCompoundDictionaryHeadsOpt Unexecuted instantiation: metablock.c:PrefetchCompoundDictionaryHeadsOpt Unexecuted instantiation: backward_references.c:PrefetchCompoundDictionaryHeadsOpt Unexecuted instantiation: backward_references_hq.c:PrefetchCompoundDictionaryHeadsOpt Unexecuted instantiation: brotli_bit_stream.c:PrefetchCompoundDictionaryHeadsOpt Unexecuted instantiation: compress_fragment.c:PrefetchCompoundDictionaryHeadsOpt Unexecuted instantiation: compress_fragment_two_pass.c:PrefetchCompoundDictionaryHeadsOpt |
816 | | |
817 | | static BROTLI_INLINE void FindCompoundDictionaryMatchOpt( |
818 | | const PreparedDictionaryView* self, |
819 | | const PreparedDictionaryProbe* BROTLI_RESTRICT probe, |
820 | | const uint8_t* BROTLI_RESTRICT data, |
821 | | const size_t ring_buffer_mask, const int* BROTLI_RESTRICT distance_cache, |
822 | | const size_t cur_ix, const size_t max_length, const size_t distance_offset, |
823 | 0 | const size_t max_distance, HasherSearchResult* BROTLI_RESTRICT out) { |
824 | 0 | const uint32_t source_size = self->source_size; |
825 | 0 | const size_t boundary = distance_offset - source_size; |
826 | |
|
827 | 0 | const uint8_t* source = self->source; |
828 | |
|
829 | 0 | const size_t cur_ix_masked = cur_ix & ring_buffer_mask; |
830 | 0 | score_t best_score = out->score; |
831 | 0 | size_t best_len = out->len; |
832 | 0 | size_t i; |
833 | | /* The hash and hashtable offsets were calculated in Prefetch, reuse the |
834 | | * results. */ |
835 | 0 | const uint32_t* BROTLI_RESTRICT chain = probe->chain; |
836 | 0 | uint32_t item = probe->item; |
837 | | #if defined(BROTLI_DEBUG) || defined(BROTLI_ENABLE_LOG) |
838 | | { |
839 | | const uint64_t bytes = BROTLI_UNALIGNED_LOAD64LE(&data[cur_ix_masked]); |
840 | | const uint64_t h = |
841 | | (bytes & self->hash_mask) * kPreparedDictionaryHashMul64Long; |
842 | | const uint32_t key = (uint32_t)(h >> self->hash_shift); |
843 | | const uint32_t slot = key & self->slot_mask; |
844 | | const uint32_t head = self->heads[key]; |
845 | | const uint32_t* expected_chain = |
846 | | &self->items[self->slot_offsets[slot] + head]; |
847 | | const uint32_t expected_item = (head == 0xFFFF) ? 1 : 0; |
848 | | BROTLI_DCHECK(probe->chain == expected_chain); |
849 | | BROTLI_DCHECK(probe->item == expected_item); |
850 | | } |
851 | | #endif |
852 | |
|
853 | 0 | for (i = 0; i < 4; ++i) { |
854 | 0 | const size_t distance = (size_t)distance_cache[i]; |
855 | 0 | size_t offset; |
856 | 0 | size_t limit; |
857 | 0 | size_t len; |
858 | 0 | if (distance <= boundary || distance > distance_offset) continue; |
859 | 0 | offset = distance_offset - distance; |
860 | 0 | limit = source_size - offset; |
861 | 0 | limit = limit > max_length ? max_length : limit; |
862 | 0 | len = FindMatchLengthWithLimit(&source[offset], &data[cur_ix_masked], |
863 | 0 | limit); |
864 | 0 | if (len >= 2) { |
865 | 0 | score_t score = BackwardReferenceScoreUsingLastDistance(len); |
866 | 0 | if (best_score < score) { |
867 | 0 | if (i != 0) score -= BackwardReferencePenaltyUsingLastDistance(i); |
868 | 0 | if (best_score < score) { |
869 | 0 | best_score = score; |
870 | 0 | if (len > best_len) best_len = len; |
871 | 0 | out->len = len; |
872 | 0 | out->len_code_delta = 0; |
873 | 0 | out->distance = distance; |
874 | 0 | out->score = best_score; |
875 | 0 | } |
876 | 0 | } |
877 | 0 | } |
878 | 0 | } |
879 | | /* we require matches of len >4, so increase best_len to 3, so we can compare |
880 | | * 4 bytes all the time. */ |
881 | 0 | if (best_len < 3) { |
882 | 0 | best_len = 3; |
883 | 0 | } |
884 | 0 | while (item == 0) { |
885 | 0 | size_t offset; |
886 | 0 | size_t distance; |
887 | 0 | size_t limit; |
888 | 0 | item = *chain; |
889 | 0 | chain++; |
890 | 0 | offset = item & 0x7FFFFFFF; |
891 | 0 | item &= 0x80000000; |
892 | 0 | distance = distance_offset - offset; |
893 | 0 | limit = source_size - offset; |
894 | 0 | limit = (limit > max_length) ? max_length : limit; |
895 | 0 | if (distance > max_distance) continue; |
896 | 0 | if (best_len >= limit || |
897 | | /* compare 4 bytes ending at best_len + 1 */ |
898 | 0 | BrotliUnalignedRead32(&data[cur_ix_masked + best_len - 3]) != |
899 | 0 | BrotliUnalignedRead32(&source[offset + best_len - 3])) { |
900 | 0 | continue; |
901 | 0 | } |
902 | 0 | { |
903 | 0 | const size_t len = FindMatchLengthWithLimit(&source[offset], |
904 | 0 | &data[cur_ix_masked], |
905 | 0 | limit); |
906 | 0 | if (len >= 4) { |
907 | 0 | score_t score = BackwardReferenceScore(len, distance); |
908 | 0 | if (best_score < score) { |
909 | 0 | best_score = score; |
910 | 0 | best_len = len; |
911 | 0 | out->len = best_len; |
912 | 0 | out->len_code_delta = 0; |
913 | 0 | out->distance = distance; |
914 | 0 | out->score = best_score; |
915 | 0 | } |
916 | 0 | } |
917 | 0 | } |
918 | 0 | } |
919 | 0 | } Unexecuted instantiation: encode.c:FindCompoundDictionaryMatchOpt Unexecuted instantiation: encoder_dict.c:FindCompoundDictionaryMatchOpt Unexecuted instantiation: metablock.c:FindCompoundDictionaryMatchOpt Unexecuted instantiation: backward_references.c:FindCompoundDictionaryMatchOpt Unexecuted instantiation: backward_references_hq.c:FindCompoundDictionaryMatchOpt Unexecuted instantiation: brotli_bit_stream.c:FindCompoundDictionaryMatchOpt Unexecuted instantiation: compress_fragment.c:FindCompoundDictionaryMatchOpt Unexecuted instantiation: compress_fragment_two_pass.c:FindCompoundDictionaryMatchOpt |
920 | | |
921 | | static BROTLI_INLINE void LookupCompoundDictionaryMatchOpt( |
922 | | const CompoundDictionary* addon, |
923 | | const PreparedDictionaryProbe* BROTLI_RESTRICT probes, |
924 | | const uint8_t* BROTLI_RESTRICT data, |
925 | | const size_t ring_buffer_mask, const int* BROTLI_RESTRICT distance_cache, |
926 | | const size_t cur_ix, const size_t max_length, |
927 | | const size_t max_ring_buffer_distance, const size_t max_distance, |
928 | 0 | HasherSearchResult* sr) { |
929 | 0 | size_t base_offset = max_ring_buffer_distance + 1 + addon->total_size - 1; |
930 | 0 | size_t d; |
931 | 0 | for (d = 0; d < addon->num_chunks; ++d) { |
932 | 0 | FindCompoundDictionaryMatchOpt( |
933 | 0 | &addon->chunk_views[d], &probes[d], data, ring_buffer_mask, |
934 | 0 | distance_cache, cur_ix, max_length, |
935 | 0 | base_offset - addon->chunk_offsets[d], max_distance, sr); |
936 | 0 | } |
937 | 0 | } Unexecuted instantiation: encode.c:LookupCompoundDictionaryMatchOpt Unexecuted instantiation: encoder_dict.c:LookupCompoundDictionaryMatchOpt Unexecuted instantiation: metablock.c:LookupCompoundDictionaryMatchOpt Unexecuted instantiation: backward_references.c:LookupCompoundDictionaryMatchOpt Unexecuted instantiation: backward_references_hq.c:LookupCompoundDictionaryMatchOpt Unexecuted instantiation: brotli_bit_stream.c:LookupCompoundDictionaryMatchOpt Unexecuted instantiation: compress_fragment.c:LookupCompoundDictionaryMatchOpt Unexecuted instantiation: compress_fragment_two_pass.c:LookupCompoundDictionaryMatchOpt |
938 | | |
939 | | #if defined(__cplusplus) || defined(c_plusplus) |
940 | | } /* extern "C" */ |
941 | | #endif |
942 | | |
943 | | #endif /* BROTLI_ENC_HASH_H_ */ |