/src/libjxl/third_party/brotli/c/enc/hash.h
Line | Count | Source (jump to first uncovered line) |
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 <stdlib.h> /* exit */ |
14 | | #include <string.h> /* memcmp, memset */ |
15 | | |
16 | | #include <brotli/types.h> |
17 | | |
18 | | #include "../common/constants.h" |
19 | | #include "../common/dictionary.h" |
20 | | #include "../common/platform.h" |
21 | | #include "compound_dictionary.h" |
22 | | #include "encoder_dict.h" |
23 | | #include "fast_log.h" |
24 | | #include "find_match_length.h" |
25 | | #include "memory.h" |
26 | | #include "quality.h" |
27 | | #include "static_dict.h" |
28 | | |
29 | | #if defined(__cplusplus) || defined(c_plusplus) |
30 | | extern "C" { |
31 | | #endif |
32 | | |
33 | | typedef struct { |
34 | | /** |
35 | | * Dynamically allocated areas; regular hasher uses one or two allocations; |
36 | | * "composite" hasher uses up to 4 allocations. |
37 | | */ |
38 | | void* extra[4]; |
39 | | |
40 | | /** |
41 | | * False before the fisrt invocation of HasherSetup (where "extra" memory) |
42 | | * is allocated. |
43 | | */ |
44 | | BROTLI_BOOL is_setup_; |
45 | | |
46 | | size_t dict_num_lookups; |
47 | | size_t dict_num_matches; |
48 | | |
49 | | BrotliHasherParams params; |
50 | | |
51 | | /** |
52 | | * False if hasher needs to be "prepared" before use (before the first |
53 | | * invocation of HasherSetup or after HasherReset). "preparation" is hasher |
54 | | * data initialization (using input ringbuffer). |
55 | | */ |
56 | | BROTLI_BOOL is_prepared_; |
57 | | } HasherCommon; |
58 | | |
59 | 0 | #define score_t size_t |
60 | | |
61 | | static const uint32_t kCutoffTransformsCount = 10; |
62 | | /* 0, 12, 27, 23, 42, 63, 56, 48, 59, 64 */ |
63 | | /* 0+0, 4+8, 8+19, 12+11, 16+26, 20+43, 24+32, 28+20, 32+27, 36+28 */ |
64 | | static const uint64_t kCutoffTransforms = |
65 | | BROTLI_MAKE_UINT64_T(0x071B520A, 0xDA2D3200); |
66 | | |
67 | | typedef struct HasherSearchResult { |
68 | | size_t len; |
69 | | size_t distance; |
70 | | score_t score; |
71 | | int len_code_delta; /* == len_code - len */ |
72 | | } HasherSearchResult; |
73 | | |
74 | | /* kHashMul32 multiplier has these properties: |
75 | | * The multiplier must be odd. Otherwise we may lose the highest bit. |
76 | | * No long streaks of ones or zeros. |
77 | | * There is no effort to ensure that it is a prime, the oddity is enough |
78 | | for this use. |
79 | | * The number has been tuned heuristically against compression benchmarks. */ |
80 | | static const uint32_t kHashMul32 = 0x1E35A7BD; |
81 | | static const uint64_t kHashMul64 = BROTLI_MAKE_UINT64_T(0x1E35A7BD, 0x1E35A7BD); |
82 | | static const uint64_t kHashMul64Long = |
83 | | BROTLI_MAKE_UINT64_T(0x1FE35A7Bu, 0xD3579BD3u); |
84 | | |
85 | 0 | static BROTLI_INLINE uint32_t Hash14(const uint8_t* data) { |
86 | 0 | uint32_t h = BROTLI_UNALIGNED_LOAD32LE(data) * kHashMul32; |
87 | | /* The higher bits contain more mixture from the multiplication, |
88 | | so we take our results from there. */ |
89 | 0 | return h >> (32 - 14); |
90 | 0 | } Unexecuted instantiation: encode.c:Hash14 Unexecuted instantiation: encoder_dict.c:Hash14 Unexecuted instantiation: backward_references_hq.c:Hash14 Unexecuted instantiation: backward_references.c:Hash14 |
91 | | |
92 | | static BROTLI_INLINE void PrepareDistanceCache( |
93 | 0 | int* BROTLI_RESTRICT distance_cache, const int num_distances) { |
94 | 0 | if (num_distances > 4) { |
95 | 0 | int last_distance = distance_cache[0]; |
96 | 0 | distance_cache[4] = last_distance - 1; |
97 | 0 | distance_cache[5] = last_distance + 1; |
98 | 0 | distance_cache[6] = last_distance - 2; |
99 | 0 | distance_cache[7] = last_distance + 2; |
100 | 0 | distance_cache[8] = last_distance - 3; |
101 | 0 | distance_cache[9] = last_distance + 3; |
102 | 0 | if (num_distances > 10) { |
103 | 0 | int next_last_distance = distance_cache[1]; |
104 | 0 | distance_cache[10] = next_last_distance - 1; |
105 | 0 | distance_cache[11] = next_last_distance + 1; |
106 | 0 | distance_cache[12] = next_last_distance - 2; |
107 | 0 | distance_cache[13] = next_last_distance + 2; |
108 | 0 | distance_cache[14] = next_last_distance - 3; |
109 | 0 | distance_cache[15] = next_last_distance + 3; |
110 | 0 | } |
111 | 0 | } |
112 | 0 | } Unexecuted instantiation: encode.c:PrepareDistanceCache Unexecuted instantiation: encoder_dict.c:PrepareDistanceCache Unexecuted instantiation: backward_references_hq.c:PrepareDistanceCache Unexecuted instantiation: backward_references.c:PrepareDistanceCache |
113 | | |
114 | 0 | #define BROTLI_LITERAL_BYTE_SCORE 135 |
115 | 0 | #define BROTLI_DISTANCE_BIT_PENALTY 30 |
116 | | /* Score must be positive after applying maximal penalty. */ |
117 | 0 | #define BROTLI_SCORE_BASE (BROTLI_DISTANCE_BIT_PENALTY * 8 * sizeof(size_t)) |
118 | | |
119 | | /* Usually, we always choose the longest backward reference. This function |
120 | | allows for the exception of that rule. |
121 | | |
122 | | If we choose a backward reference that is further away, it will |
123 | | usually be coded with more bits. We approximate this by assuming |
124 | | log2(distance). If the distance can be expressed in terms of the |
125 | | last four distances, we use some heuristic constants to estimate |
126 | | the bits cost. For the first up to four literals we use the bit |
127 | | cost of the literals from the literal cost model, after that we |
128 | | use the average bit cost of the cost model. |
129 | | |
130 | | This function is used to sometimes discard a longer backward reference |
131 | | when it is not much longer and the bit cost for encoding it is more |
132 | | than the saved literals. |
133 | | |
134 | | backward_reference_offset MUST be positive. */ |
135 | | static BROTLI_INLINE score_t BackwardReferenceScore( |
136 | 0 | size_t copy_length, size_t backward_reference_offset) { |
137 | 0 | return BROTLI_SCORE_BASE + BROTLI_LITERAL_BYTE_SCORE * (score_t)copy_length - |
138 | 0 | BROTLI_DISTANCE_BIT_PENALTY * Log2FloorNonZero(backward_reference_offset); |
139 | 0 | } Unexecuted instantiation: encode.c:BackwardReferenceScore Unexecuted instantiation: encoder_dict.c:BackwardReferenceScore Unexecuted instantiation: backward_references_hq.c:BackwardReferenceScore Unexecuted instantiation: backward_references.c:BackwardReferenceScore |
140 | | |
141 | | static BROTLI_INLINE score_t BackwardReferenceScoreUsingLastDistance( |
142 | 0 | size_t copy_length) { |
143 | 0 | return BROTLI_LITERAL_BYTE_SCORE * (score_t)copy_length + |
144 | 0 | BROTLI_SCORE_BASE + 15; |
145 | 0 | } Unexecuted instantiation: encode.c:BackwardReferenceScoreUsingLastDistance Unexecuted instantiation: encoder_dict.c:BackwardReferenceScoreUsingLastDistance Unexecuted instantiation: backward_references_hq.c:BackwardReferenceScoreUsingLastDistance Unexecuted instantiation: backward_references.c:BackwardReferenceScoreUsingLastDistance |
146 | | |
147 | | static BROTLI_INLINE score_t BackwardReferencePenaltyUsingLastDistance( |
148 | 0 | size_t distance_short_code) { |
149 | 0 | return (score_t)39 + ((0x1CA10 >> (distance_short_code & 0xE)) & 0xE); |
150 | 0 | } Unexecuted instantiation: encode.c:BackwardReferencePenaltyUsingLastDistance Unexecuted instantiation: encoder_dict.c:BackwardReferencePenaltyUsingLastDistance Unexecuted instantiation: backward_references_hq.c:BackwardReferencePenaltyUsingLastDistance Unexecuted instantiation: backward_references.c:BackwardReferencePenaltyUsingLastDistance |
151 | | |
152 | | static BROTLI_INLINE BROTLI_BOOL TestStaticDictionaryItem( |
153 | | const BrotliEncoderDictionary* dictionary, size_t len, size_t word_idx, |
154 | | const uint8_t* data, size_t max_length, size_t max_backward, |
155 | 0 | size_t max_distance, HasherSearchResult* out) { |
156 | 0 | size_t offset; |
157 | 0 | size_t matchlen; |
158 | 0 | size_t backward; |
159 | 0 | score_t score; |
160 | 0 | offset = dictionary->words->offsets_by_length[len] + len * word_idx; |
161 | 0 | if (len > max_length) { |
162 | 0 | return BROTLI_FALSE; |
163 | 0 | } |
164 | | |
165 | 0 | matchlen = |
166 | 0 | FindMatchLengthWithLimit(data, &dictionary->words->data[offset], len); |
167 | 0 | if (matchlen + dictionary->cutoffTransformsCount <= len || matchlen == 0) { |
168 | 0 | return BROTLI_FALSE; |
169 | 0 | } |
170 | 0 | { |
171 | 0 | size_t cut = len - matchlen; |
172 | 0 | size_t transform_id = (cut << 2) + |
173 | 0 | (size_t)((dictionary->cutoffTransforms >> (cut * 6)) & 0x3F); |
174 | 0 | backward = max_backward + 1 + word_idx + |
175 | 0 | (transform_id << dictionary->words->size_bits_by_length[len]); |
176 | 0 | } |
177 | 0 | if (backward > max_distance) { |
178 | 0 | return BROTLI_FALSE; |
179 | 0 | } |
180 | 0 | score = BackwardReferenceScore(matchlen, backward); |
181 | 0 | if (score < out->score) { |
182 | 0 | return BROTLI_FALSE; |
183 | 0 | } |
184 | 0 | out->len = matchlen; |
185 | 0 | out->len_code_delta = (int)len - (int)matchlen; |
186 | 0 | out->distance = backward; |
187 | 0 | out->score = score; |
188 | 0 | return BROTLI_TRUE; |
189 | 0 | } Unexecuted instantiation: encode.c:TestStaticDictionaryItem Unexecuted instantiation: encoder_dict.c:TestStaticDictionaryItem Unexecuted instantiation: backward_references_hq.c:TestStaticDictionaryItem Unexecuted instantiation: backward_references.c:TestStaticDictionaryItem |
190 | | |
191 | | static BROTLI_INLINE void SearchInStaticDictionary( |
192 | | const BrotliEncoderDictionary* dictionary, |
193 | | HasherCommon* common, const uint8_t* data, size_t max_length, |
194 | | size_t max_backward, size_t max_distance, |
195 | 0 | HasherSearchResult* out, BROTLI_BOOL shallow) { |
196 | 0 | size_t key; |
197 | 0 | size_t i; |
198 | 0 | if (common->dict_num_matches < (common->dict_num_lookups >> 7)) { |
199 | 0 | return; |
200 | 0 | } |
201 | 0 | key = Hash14(data) << 1; |
202 | 0 | for (i = 0; i < (shallow ? 1u : 2u); ++i, ++key) { |
203 | 0 | common->dict_num_lookups++; |
204 | 0 | if (dictionary->hash_table_lengths[key] != 0) { |
205 | 0 | BROTLI_BOOL item_matches = TestStaticDictionaryItem( |
206 | 0 | dictionary, dictionary->hash_table_lengths[key], |
207 | 0 | dictionary->hash_table_words[key], data, |
208 | 0 | max_length, max_backward, max_distance, out); |
209 | 0 | if (item_matches) { |
210 | 0 | common->dict_num_matches++; |
211 | 0 | } |
212 | 0 | } |
213 | 0 | } |
214 | 0 | } Unexecuted instantiation: encode.c:SearchInStaticDictionary Unexecuted instantiation: encoder_dict.c:SearchInStaticDictionary Unexecuted instantiation: backward_references_hq.c:SearchInStaticDictionary Unexecuted instantiation: backward_references.c:SearchInStaticDictionary |
215 | | |
216 | | typedef struct BackwardMatch { |
217 | | uint32_t distance; |
218 | | uint32_t length_and_code; |
219 | | } BackwardMatch; |
220 | | |
221 | | static BROTLI_INLINE void InitBackwardMatch(BackwardMatch* self, |
222 | 0 | size_t dist, size_t len) { |
223 | 0 | self->distance = (uint32_t)dist; |
224 | 0 | self->length_and_code = (uint32_t)(len << 5); |
225 | 0 | } Unexecuted instantiation: encode.c:InitBackwardMatch Unexecuted instantiation: encoder_dict.c:InitBackwardMatch Unexecuted instantiation: backward_references_hq.c:InitBackwardMatch Unexecuted instantiation: backward_references.c:InitBackwardMatch |
226 | | |
227 | | static BROTLI_INLINE void InitDictionaryBackwardMatch(BackwardMatch* self, |
228 | 0 | size_t dist, size_t len, size_t len_code) { |
229 | 0 | self->distance = (uint32_t)dist; |
230 | 0 | self->length_and_code = |
231 | 0 | (uint32_t)((len << 5) | (len == len_code ? 0 : len_code)); |
232 | 0 | } Unexecuted instantiation: encode.c:InitDictionaryBackwardMatch Unexecuted instantiation: encoder_dict.c:InitDictionaryBackwardMatch Unexecuted instantiation: backward_references_hq.c:InitDictionaryBackwardMatch Unexecuted instantiation: backward_references.c:InitDictionaryBackwardMatch |
233 | | |
234 | 0 | static BROTLI_INLINE size_t BackwardMatchLength(const BackwardMatch* self) { |
235 | 0 | return self->length_and_code >> 5; |
236 | 0 | } Unexecuted instantiation: encode.c:BackwardMatchLength Unexecuted instantiation: encoder_dict.c:BackwardMatchLength Unexecuted instantiation: backward_references_hq.c:BackwardMatchLength Unexecuted instantiation: backward_references.c:BackwardMatchLength |
237 | | |
238 | 0 | static BROTLI_INLINE size_t BackwardMatchLengthCode(const BackwardMatch* self) { |
239 | 0 | size_t code = self->length_and_code & 31; |
240 | 0 | return code ? code : BackwardMatchLength(self); |
241 | 0 | } Unexecuted instantiation: encode.c:BackwardMatchLengthCode Unexecuted instantiation: encoder_dict.c:BackwardMatchLengthCode Unexecuted instantiation: backward_references_hq.c:BackwardMatchLengthCode Unexecuted instantiation: backward_references.c:BackwardMatchLengthCode |
242 | | |
243 | 0 | #define EXPAND_CAT(a, b) CAT(a, b) |
244 | 0 | #define CAT(a, b) a ## b |
245 | 0 | #define FN(X) EXPAND_CAT(X, HASHER()) |
246 | | |
247 | | #define HASHER() H10 |
248 | 0 | #define BUCKET_BITS 17 |
249 | 0 | #define MAX_TREE_SEARCH_DEPTH 64 |
250 | 0 | #define MAX_TREE_COMP_LENGTH 128 |
251 | | #include "hash_to_binary_tree_inc.h" /* NOLINT(build/include) */ |
252 | | #undef MAX_TREE_SEARCH_DEPTH |
253 | | #undef MAX_TREE_COMP_LENGTH |
254 | | #undef BUCKET_BITS |
255 | | #undef HASHER |
256 | | /* MAX_NUM_MATCHES == 64 + MAX_TREE_SEARCH_DEPTH */ |
257 | 0 | #define MAX_NUM_MATCHES_H10 128 |
258 | | |
259 | | /* For BUCKET_SWEEP_BITS == 0, enabling the dictionary lookup makes compression |
260 | | a little faster (0.5% - 1%) and it compresses 0.15% better on small text |
261 | | and HTML inputs. */ |
262 | | |
263 | | #define HASHER() H2 |
264 | 0 | #define BUCKET_BITS 16 |
265 | 0 | #define BUCKET_SWEEP_BITS 0 |
266 | 0 | #define HASH_LEN 5 |
267 | 0 | #define USE_DICTIONARY 1 |
268 | | #include "hash_longest_match_quickly_inc.h" /* NOLINT(build/include) */ |
269 | | #undef BUCKET_SWEEP_BITS |
270 | | #undef USE_DICTIONARY |
271 | | #undef HASHER |
272 | | |
273 | | #define HASHER() H3 |
274 | 0 | #define BUCKET_SWEEP_BITS 1 |
275 | 0 | #define USE_DICTIONARY 0 |
276 | | #include "hash_longest_match_quickly_inc.h" /* NOLINT(build/include) */ |
277 | | #undef USE_DICTIONARY |
278 | | #undef BUCKET_SWEEP_BITS |
279 | | #undef BUCKET_BITS |
280 | | #undef HASHER |
281 | | |
282 | | #define HASHER() H4 |
283 | 0 | #define BUCKET_BITS 17 |
284 | 0 | #define BUCKET_SWEEP_BITS 2 |
285 | 0 | #define USE_DICTIONARY 1 |
286 | | #include "hash_longest_match_quickly_inc.h" /* NOLINT(build/include) */ |
287 | | #undef USE_DICTIONARY |
288 | | #undef HASH_LEN |
289 | | #undef BUCKET_SWEEP_BITS |
290 | | #undef BUCKET_BITS |
291 | | #undef HASHER |
292 | | |
293 | | #define HASHER() H5 |
294 | | #include "hash_longest_match_inc.h" /* NOLINT(build/include) */ |
295 | | #undef HASHER |
296 | | |
297 | | #define HASHER() H6 |
298 | | #include "hash_longest_match64_inc.h" /* NOLINT(build/include) */ |
299 | | #undef HASHER |
300 | | |
301 | 0 | #define BUCKET_BITS 15 |
302 | | |
303 | 0 | #define NUM_LAST_DISTANCES_TO_CHECK 4 |
304 | 0 | #define NUM_BANKS 1 |
305 | 0 | #define BANK_BITS 16 |
306 | | #define HASHER() H40 |
307 | | #include "hash_forgetful_chain_inc.h" /* NOLINT(build/include) */ |
308 | | #undef HASHER |
309 | | #undef NUM_LAST_DISTANCES_TO_CHECK |
310 | | |
311 | 0 | #define NUM_LAST_DISTANCES_TO_CHECK 10 |
312 | | #define HASHER() H41 |
313 | | #include "hash_forgetful_chain_inc.h" /* NOLINT(build/include) */ |
314 | | #undef HASHER |
315 | | #undef NUM_LAST_DISTANCES_TO_CHECK |
316 | | #undef NUM_BANKS |
317 | | #undef BANK_BITS |
318 | | |
319 | 0 | #define NUM_LAST_DISTANCES_TO_CHECK 16 |
320 | 0 | #define NUM_BANKS 512 |
321 | 0 | #define BANK_BITS 9 |
322 | | #define HASHER() H42 |
323 | | #include "hash_forgetful_chain_inc.h" /* NOLINT(build/include) */ |
324 | | #undef HASHER |
325 | | #undef NUM_LAST_DISTANCES_TO_CHECK |
326 | | #undef NUM_BANKS |
327 | | #undef BANK_BITS |
328 | | |
329 | | #undef BUCKET_BITS |
330 | | |
331 | | #define HASHER() H54 |
332 | 0 | #define BUCKET_BITS 20 |
333 | 0 | #define BUCKET_SWEEP_BITS 2 |
334 | 0 | #define HASH_LEN 7 |
335 | 0 | #define USE_DICTIONARY 0 |
336 | | #include "hash_longest_match_quickly_inc.h" /* NOLINT(build/include) */ |
337 | | #undef USE_DICTIONARY |
338 | | #undef HASH_LEN |
339 | | #undef BUCKET_SWEEP_BITS |
340 | | #undef BUCKET_BITS |
341 | | #undef HASHER |
342 | | |
343 | | /* fast large window hashers */ |
344 | | |
345 | | #define HASHER() HROLLING_FAST |
346 | 0 | #define CHUNKLEN 32 |
347 | 0 | #define JUMP 4 |
348 | 0 | #define NUMBUCKETS 16777216 |
349 | 0 | #define MASK ((NUMBUCKETS * 64) - 1) |
350 | | #include "hash_rolling_inc.h" /* NOLINT(build/include) */ |
351 | | #undef JUMP |
352 | | #undef HASHER |
353 | | |
354 | | |
355 | | #define HASHER() HROLLING |
356 | 0 | #define JUMP 1 |
357 | | #include "hash_rolling_inc.h" /* NOLINT(build/include) */ |
358 | | #undef MASK |
359 | | #undef NUMBUCKETS |
360 | | #undef JUMP |
361 | | #undef CHUNKLEN |
362 | | #undef HASHER |
363 | | |
364 | | #define HASHER() H35 |
365 | | #define HASHER_A H3 |
366 | | #define HASHER_B HROLLING_FAST |
367 | | #include "hash_composite_inc.h" /* NOLINT(build/include) */ |
368 | | #undef HASHER_A |
369 | | #undef HASHER_B |
370 | | #undef HASHER |
371 | | |
372 | | #define HASHER() H55 |
373 | | #define HASHER_A H54 |
374 | | #define HASHER_B HROLLING_FAST |
375 | | #include "hash_composite_inc.h" /* NOLINT(build/include) */ |
376 | | #undef HASHER_A |
377 | | #undef HASHER_B |
378 | | #undef HASHER |
379 | | |
380 | | #define HASHER() H65 |
381 | | #define HASHER_A H6 |
382 | | #define HASHER_B HROLLING |
383 | | #include "hash_composite_inc.h" /* NOLINT(build/include) */ |
384 | | #undef HASHER_A |
385 | | #undef HASHER_B |
386 | | #undef HASHER |
387 | | |
388 | | #undef FN |
389 | | #undef CAT |
390 | | #undef EXPAND_CAT |
391 | | |
392 | 0 | #define FOR_SIMPLE_HASHERS(H) H(2) H(3) H(4) H(5) H(6) H(40) H(41) H(42) H(54) |
393 | 0 | #define FOR_COMPOSITE_HASHERS(H) H(35) H(55) H(65) |
394 | 0 | #define FOR_GENERIC_HASHERS(H) FOR_SIMPLE_HASHERS(H) FOR_COMPOSITE_HASHERS(H) |
395 | 0 | #define FOR_ALL_HASHERS(H) FOR_GENERIC_HASHERS(H) H(10) |
396 | | |
397 | | typedef struct { |
398 | | HasherCommon common; |
399 | | |
400 | | union { |
401 | | #define MEMBER_(N) \ |
402 | | H ## N _H ## N; |
403 | | FOR_ALL_HASHERS(MEMBER_) |
404 | | #undef MEMBER_ |
405 | | } privat; |
406 | | } Hasher; |
407 | | |
408 | | /* MUST be invoked before any other method. */ |
409 | 0 | static BROTLI_INLINE void HasherInit(Hasher* hasher) { |
410 | 0 | hasher->common.is_setup_ = BROTLI_FALSE; |
411 | 0 | hasher->common.extra[0] = NULL; |
412 | 0 | hasher->common.extra[1] = NULL; |
413 | 0 | hasher->common.extra[2] = NULL; |
414 | 0 | hasher->common.extra[3] = NULL; |
415 | 0 | } Unexecuted instantiation: encode.c:HasherInit Unexecuted instantiation: encoder_dict.c:HasherInit Unexecuted instantiation: backward_references_hq.c:HasherInit Unexecuted instantiation: backward_references.c:HasherInit |
416 | | |
417 | 0 | static BROTLI_INLINE void DestroyHasher(MemoryManager* m, Hasher* hasher) { |
418 | 0 | if (hasher->common.extra[0] != NULL) BROTLI_FREE(m, hasher->common.extra[0]); |
419 | 0 | if (hasher->common.extra[1] != NULL) BROTLI_FREE(m, hasher->common.extra[1]); |
420 | 0 | if (hasher->common.extra[2] != NULL) BROTLI_FREE(m, hasher->common.extra[2]); |
421 | 0 | if (hasher->common.extra[3] != NULL) BROTLI_FREE(m, hasher->common.extra[3]); |
422 | 0 | } Unexecuted instantiation: encode.c:DestroyHasher Unexecuted instantiation: encoder_dict.c:DestroyHasher Unexecuted instantiation: backward_references_hq.c:DestroyHasher Unexecuted instantiation: backward_references.c:DestroyHasher |
423 | | |
424 | 0 | static BROTLI_INLINE void HasherReset(Hasher* hasher) { |
425 | 0 | hasher->common.is_prepared_ = BROTLI_FALSE; |
426 | 0 | } Unexecuted instantiation: encode.c:HasherReset Unexecuted instantiation: encoder_dict.c:HasherReset Unexecuted instantiation: backward_references_hq.c:HasherReset Unexecuted instantiation: backward_references.c:HasherReset |
427 | | |
428 | | static BROTLI_INLINE void HasherSize(const BrotliEncoderParams* params, |
429 | 0 | BROTLI_BOOL one_shot, const size_t input_size, size_t* alloc_size) { |
430 | 0 | switch (params->hasher.type) { |
431 | 0 | #define SIZE_(N) \ |
432 | 0 | case N: \ |
433 | 0 | HashMemAllocInBytesH ## N(params, one_shot, input_size, alloc_size); \ |
434 | 0 | break; |
435 | 0 | FOR_ALL_HASHERS(SIZE_) |
436 | 0 | #undef SIZE_ |
437 | 0 | default: |
438 | 0 | break; |
439 | 0 | } |
440 | 0 | } Unexecuted instantiation: encode.c:HasherSize Unexecuted instantiation: encoder_dict.c:HasherSize Unexecuted instantiation: backward_references_hq.c:HasherSize Unexecuted instantiation: backward_references.c:HasherSize |
441 | | |
442 | | static BROTLI_INLINE void HasherSetup(MemoryManager* m, Hasher* hasher, |
443 | | BrotliEncoderParams* params, const uint8_t* data, size_t position, |
444 | 0 | size_t input_size, BROTLI_BOOL is_last) { |
445 | 0 | BROTLI_BOOL one_shot = (position == 0 && is_last); |
446 | 0 | if (!hasher->common.is_setup_) { |
447 | 0 | size_t alloc_size[4] = {0}; |
448 | 0 | size_t i; |
449 | 0 | ChooseHasher(params, ¶ms->hasher); |
450 | 0 | hasher->common.params = params->hasher; |
451 | 0 | hasher->common.dict_num_lookups = 0; |
452 | 0 | hasher->common.dict_num_matches = 0; |
453 | 0 | HasherSize(params, one_shot, input_size, alloc_size); |
454 | 0 | for (i = 0; i < 4; ++i) { |
455 | 0 | if (alloc_size[i] == 0) continue; |
456 | 0 | hasher->common.extra[i] = BROTLI_ALLOC(m, uint8_t, alloc_size[i]); |
457 | 0 | if (BROTLI_IS_OOM(m) || BROTLI_IS_NULL(hasher->common.extra[i])) return; |
458 | 0 | } |
459 | 0 | switch (hasher->common.params.type) { |
460 | 0 | #define INITIALIZE_(N) \ |
461 | 0 | case N: \ |
462 | 0 | InitializeH ## N(&hasher->common, \ |
463 | 0 | &hasher->privat._H ## N, params); \ |
464 | 0 | break; |
465 | 0 | FOR_ALL_HASHERS(INITIALIZE_); |
466 | 0 | #undef INITIALIZE_ |
467 | 0 | default: |
468 | 0 | break; |
469 | 0 | } |
470 | 0 | HasherReset(hasher); |
471 | 0 | hasher->common.is_setup_ = BROTLI_TRUE; |
472 | 0 | } |
473 | | |
474 | 0 | if (!hasher->common.is_prepared_) { |
475 | 0 | switch (hasher->common.params.type) { |
476 | 0 | #define PREPARE_(N) \ |
477 | 0 | case N: \ |
478 | 0 | PrepareH ## N( \ |
479 | 0 | &hasher->privat._H ## N, \ |
480 | 0 | one_shot, input_size, data); \ |
481 | 0 | break; |
482 | 0 | FOR_ALL_HASHERS(PREPARE_) |
483 | 0 | #undef PREPARE_ |
484 | 0 | default: break; |
485 | 0 | } |
486 | 0 | hasher->common.is_prepared_ = BROTLI_TRUE; |
487 | 0 | } |
488 | 0 | } Unexecuted instantiation: encode.c:HasherSetup Unexecuted instantiation: encoder_dict.c:HasherSetup Unexecuted instantiation: backward_references_hq.c:HasherSetup Unexecuted instantiation: backward_references.c:HasherSetup |
489 | | |
490 | | static BROTLI_INLINE void InitOrStitchToPreviousBlock( |
491 | | MemoryManager* m, Hasher* hasher, const uint8_t* data, size_t mask, |
492 | | BrotliEncoderParams* params, size_t position, size_t input_size, |
493 | 0 | BROTLI_BOOL is_last) { |
494 | 0 | HasherSetup(m, hasher, params, data, position, input_size, is_last); |
495 | 0 | if (BROTLI_IS_OOM(m)) return; |
496 | 0 | switch (hasher->common.params.type) { |
497 | 0 | #define INIT_(N) \ |
498 | 0 | case N: \ |
499 | 0 | StitchToPreviousBlockH ## N( \ |
500 | 0 | &hasher->privat._H ## N, \ |
501 | 0 | input_size, position, data, mask); \ |
502 | 0 | break; |
503 | 0 | FOR_ALL_HASHERS(INIT_) |
504 | 0 | #undef INIT_ |
505 | 0 | default: break; |
506 | 0 | } |
507 | 0 | } Unexecuted instantiation: encode.c:InitOrStitchToPreviousBlock Unexecuted instantiation: encoder_dict.c:InitOrStitchToPreviousBlock Unexecuted instantiation: backward_references_hq.c:InitOrStitchToPreviousBlock Unexecuted instantiation: backward_references.c:InitOrStitchToPreviousBlock |
508 | | |
509 | | /* NB: when seamless dictionary-ring-buffer copies are implemented, don't forget |
510 | | to add proper guards for non-zero-BROTLI_PARAM_STREAM_OFFSET. */ |
511 | | static BROTLI_INLINE void FindCompoundDictionaryMatch( |
512 | | const PreparedDictionary* self, const uint8_t* BROTLI_RESTRICT data, |
513 | | const size_t ring_buffer_mask, const int* BROTLI_RESTRICT distance_cache, |
514 | | const size_t cur_ix, const size_t max_length, const size_t distance_offset, |
515 | 0 | const size_t max_distance, HasherSearchResult* BROTLI_RESTRICT out) { |
516 | 0 | const uint32_t source_size = self->source_size; |
517 | 0 | const size_t boundary = distance_offset - source_size; |
518 | 0 | const uint32_t hash_bits = self->hash_bits; |
519 | 0 | const uint32_t bucket_bits = self->bucket_bits; |
520 | 0 | const uint32_t slot_bits = self->slot_bits; |
521 | |
|
522 | 0 | const uint32_t hash_shift = 64u - bucket_bits; |
523 | 0 | const uint32_t slot_mask = (~((uint32_t)0U)) >> (32 - slot_bits); |
524 | 0 | const uint64_t hash_mask = (~((uint64_t)0U)) >> (64 - hash_bits); |
525 | |
|
526 | 0 | const uint32_t* slot_offsets = (uint32_t*)(&self[1]); |
527 | 0 | const uint16_t* heads = (uint16_t*)(&slot_offsets[1u << slot_bits]); |
528 | 0 | const uint32_t* items = (uint32_t*)(&heads[1u << bucket_bits]); |
529 | 0 | const uint8_t* source = NULL; |
530 | |
|
531 | 0 | const size_t cur_ix_masked = cur_ix & ring_buffer_mask; |
532 | 0 | score_t best_score = out->score; |
533 | 0 | size_t best_len = out->len; |
534 | 0 | size_t i; |
535 | 0 | const uint64_t h = |
536 | 0 | (BROTLI_UNALIGNED_LOAD64LE(&data[cur_ix_masked]) & hash_mask) * |
537 | 0 | kPreparedDictionaryHashMul64Long; |
538 | 0 | const uint32_t key = (uint32_t)(h >> hash_shift); |
539 | 0 | const uint32_t slot = key & slot_mask; |
540 | 0 | const uint32_t head = heads[key]; |
541 | 0 | const uint32_t* BROTLI_RESTRICT chain = &items[slot_offsets[slot] + head]; |
542 | 0 | uint32_t item = (head == 0xFFFF) ? 1 : 0; |
543 | |
|
544 | 0 | const void* tail = (void*)&items[self->num_items]; |
545 | 0 | if (self->magic == kPreparedDictionaryMagic) { |
546 | 0 | source = (const uint8_t*)tail; |
547 | 0 | } else { |
548 | | /* kLeanPreparedDictionaryMagic */ |
549 | 0 | source = (const uint8_t*)BROTLI_UNALIGNED_LOAD_PTR((const uint8_t**)tail); |
550 | 0 | } |
551 | |
|
552 | 0 | for (i = 0; i < 4; ++i) { |
553 | 0 | const size_t distance = (size_t)distance_cache[i]; |
554 | 0 | size_t offset; |
555 | 0 | size_t limit; |
556 | 0 | size_t len; |
557 | 0 | if (distance <= boundary || distance > distance_offset) continue; |
558 | 0 | offset = distance_offset - distance; |
559 | 0 | limit = source_size - offset; |
560 | 0 | limit = limit > max_length ? max_length : limit; |
561 | 0 | len = FindMatchLengthWithLimit(&source[offset], &data[cur_ix_masked], |
562 | 0 | limit); |
563 | 0 | if (len >= 2) { |
564 | 0 | score_t score = BackwardReferenceScoreUsingLastDistance(len); |
565 | 0 | if (best_score < score) { |
566 | 0 | if (i != 0) score -= BackwardReferencePenaltyUsingLastDistance(i); |
567 | 0 | if (best_score < score) { |
568 | 0 | best_score = score; |
569 | 0 | if (len > best_len) best_len = len; |
570 | 0 | out->len = len; |
571 | 0 | out->len_code_delta = 0; |
572 | 0 | out->distance = distance; |
573 | 0 | out->score = best_score; |
574 | 0 | } |
575 | 0 | } |
576 | 0 | } |
577 | 0 | } |
578 | 0 | while (item == 0) { |
579 | 0 | size_t offset; |
580 | 0 | size_t distance; |
581 | 0 | size_t limit; |
582 | 0 | item = *chain; |
583 | 0 | chain++; |
584 | 0 | offset = item & 0x7FFFFFFF; |
585 | 0 | item &= 0x80000000; |
586 | 0 | distance = distance_offset - offset; |
587 | 0 | limit = source_size - offset; |
588 | 0 | limit = (limit > max_length) ? max_length : limit; |
589 | 0 | if (distance > max_distance) continue; |
590 | 0 | if (cur_ix_masked + best_len > ring_buffer_mask || |
591 | 0 | best_len >= limit || |
592 | 0 | data[cur_ix_masked + best_len] != source[offset + best_len]) { |
593 | 0 | continue; |
594 | 0 | } |
595 | 0 | { |
596 | 0 | const size_t len = FindMatchLengthWithLimit(&source[offset], |
597 | 0 | &data[cur_ix_masked], |
598 | 0 | limit); |
599 | 0 | if (len >= 4) { |
600 | 0 | score_t score = BackwardReferenceScore(len, distance); |
601 | 0 | if (best_score < score) { |
602 | 0 | best_score = score; |
603 | 0 | best_len = len; |
604 | 0 | out->len = best_len; |
605 | 0 | out->len_code_delta = 0; |
606 | 0 | out->distance = distance; |
607 | 0 | out->score = best_score; |
608 | 0 | } |
609 | 0 | } |
610 | 0 | } |
611 | 0 | } |
612 | 0 | } Unexecuted instantiation: encode.c:FindCompoundDictionaryMatch Unexecuted instantiation: encoder_dict.c:FindCompoundDictionaryMatch Unexecuted instantiation: backward_references_hq.c:FindCompoundDictionaryMatch Unexecuted instantiation: backward_references.c:FindCompoundDictionaryMatch |
613 | | |
614 | | /* NB: when seamless dictionary-ring-buffer copies are implemented, don't forget |
615 | | to add proper guards for non-zero-BROTLI_PARAM_STREAM_OFFSET. */ |
616 | | static BROTLI_INLINE size_t FindAllCompoundDictionaryMatches( |
617 | | const PreparedDictionary* self, const uint8_t* BROTLI_RESTRICT data, |
618 | | const size_t ring_buffer_mask, const size_t cur_ix, const size_t min_length, |
619 | | const size_t max_length, const size_t distance_offset, |
620 | 0 | const size_t max_distance, BackwardMatch* matches, size_t match_limit) { |
621 | 0 | const uint32_t source_size = self->source_size; |
622 | 0 | const uint32_t hash_bits = self->hash_bits; |
623 | 0 | const uint32_t bucket_bits = self->bucket_bits; |
624 | 0 | const uint32_t slot_bits = self->slot_bits; |
625 | |
|
626 | 0 | const uint32_t hash_shift = 64u - bucket_bits; |
627 | 0 | const uint32_t slot_mask = (~((uint32_t)0U)) >> (32 - slot_bits); |
628 | 0 | const uint64_t hash_mask = (~((uint64_t)0U)) >> (64 - hash_bits); |
629 | |
|
630 | 0 | const uint32_t* slot_offsets = (uint32_t*)(&self[1]); |
631 | 0 | const uint16_t* heads = (uint16_t*)(&slot_offsets[1u << slot_bits]); |
632 | 0 | const uint32_t* items = (uint32_t*)(&heads[1u << bucket_bits]); |
633 | 0 | const uint8_t* source = NULL; |
634 | |
|
635 | 0 | const size_t cur_ix_masked = cur_ix & ring_buffer_mask; |
636 | 0 | size_t best_len = min_length; |
637 | 0 | const uint64_t h = |
638 | 0 | (BROTLI_UNALIGNED_LOAD64LE(&data[cur_ix_masked]) & hash_mask) * |
639 | 0 | kPreparedDictionaryHashMul64Long; |
640 | 0 | const uint32_t key = (uint32_t)(h >> hash_shift); |
641 | 0 | const uint32_t slot = key & slot_mask; |
642 | 0 | const uint32_t head = heads[key]; |
643 | 0 | const uint32_t* BROTLI_RESTRICT chain = &items[slot_offsets[slot] + head]; |
644 | 0 | uint32_t item = (head == 0xFFFF) ? 1 : 0; |
645 | 0 | size_t found = 0; |
646 | |
|
647 | 0 | const void* tail = (void*)&items[self->num_items]; |
648 | 0 | if (self->magic == kPreparedDictionaryMagic) { |
649 | 0 | source = (const uint8_t*)tail; |
650 | 0 | } else { |
651 | | /* kLeanPreparedDictionaryMagic */ |
652 | 0 | source = (const uint8_t*)BROTLI_UNALIGNED_LOAD_PTR((const uint8_t**)tail); |
653 | 0 | } |
654 | |
|
655 | 0 | while (item == 0) { |
656 | 0 | size_t offset; |
657 | 0 | size_t distance; |
658 | 0 | size_t limit; |
659 | 0 | size_t len; |
660 | 0 | item = *chain; |
661 | 0 | chain++; |
662 | 0 | offset = item & 0x7FFFFFFF; |
663 | 0 | item &= 0x80000000; |
664 | 0 | distance = distance_offset - offset; |
665 | 0 | limit = source_size - offset; |
666 | 0 | limit = (limit > max_length) ? max_length : limit; |
667 | 0 | if (distance > max_distance) continue; |
668 | 0 | if (cur_ix_masked + best_len > ring_buffer_mask || |
669 | 0 | best_len >= limit || |
670 | 0 | data[cur_ix_masked + best_len] != source[offset + best_len]) { |
671 | 0 | continue; |
672 | 0 | } |
673 | 0 | len = FindMatchLengthWithLimit( |
674 | 0 | &source[offset], &data[cur_ix_masked], limit); |
675 | 0 | if (len > best_len) { |
676 | 0 | best_len = len; |
677 | 0 | InitBackwardMatch(matches++, distance, len); |
678 | 0 | found++; |
679 | 0 | if (found == match_limit) break; |
680 | 0 | } |
681 | 0 | } |
682 | 0 | return found; |
683 | 0 | } Unexecuted instantiation: encode.c:FindAllCompoundDictionaryMatches Unexecuted instantiation: encoder_dict.c:FindAllCompoundDictionaryMatches Unexecuted instantiation: backward_references_hq.c:FindAllCompoundDictionaryMatches Unexecuted instantiation: backward_references.c:FindAllCompoundDictionaryMatches |
684 | | |
685 | | static BROTLI_INLINE void LookupCompoundDictionaryMatch( |
686 | | const CompoundDictionary* addon, const uint8_t* BROTLI_RESTRICT data, |
687 | | const size_t ring_buffer_mask, const int* BROTLI_RESTRICT distance_cache, |
688 | | const size_t cur_ix, const size_t max_length, |
689 | | const size_t max_ring_buffer_distance, const size_t max_distance, |
690 | 0 | HasherSearchResult* sr) { |
691 | 0 | size_t base_offset = max_ring_buffer_distance + 1 + addon->total_size - 1; |
692 | 0 | size_t d; |
693 | 0 | for (d = 0; d < addon->num_chunks; ++d) { |
694 | | /* Only one prepared dictionary type is currently supported. */ |
695 | 0 | FindCompoundDictionaryMatch( |
696 | 0 | (const PreparedDictionary*)addon->chunks[d], data, ring_buffer_mask, |
697 | 0 | distance_cache, cur_ix, max_length, |
698 | 0 | base_offset - addon->chunk_offsets[d], max_distance, sr); |
699 | 0 | } |
700 | 0 | } Unexecuted instantiation: encode.c:LookupCompoundDictionaryMatch Unexecuted instantiation: encoder_dict.c:LookupCompoundDictionaryMatch Unexecuted instantiation: backward_references_hq.c:LookupCompoundDictionaryMatch Unexecuted instantiation: backward_references.c:LookupCompoundDictionaryMatch |
701 | | |
702 | | static BROTLI_INLINE size_t LookupAllCompoundDictionaryMatches( |
703 | | const CompoundDictionary* addon, const uint8_t* BROTLI_RESTRICT data, |
704 | | const size_t ring_buffer_mask, const size_t cur_ix, size_t min_length, |
705 | | const size_t max_length, const size_t max_ring_buffer_distance, |
706 | | const size_t max_distance, BackwardMatch* matches, |
707 | 0 | size_t match_limit) { |
708 | 0 | size_t base_offset = max_ring_buffer_distance + 1 + addon->total_size - 1; |
709 | 0 | size_t d; |
710 | 0 | size_t total_found = 0; |
711 | 0 | for (d = 0; d < addon->num_chunks; ++d) { |
712 | | /* Only one prepared dictionary type is currently supported. */ |
713 | 0 | total_found += FindAllCompoundDictionaryMatches( |
714 | 0 | (const PreparedDictionary*)addon->chunks[d], data, ring_buffer_mask, |
715 | 0 | cur_ix, min_length, max_length, base_offset - addon->chunk_offsets[d], |
716 | 0 | max_distance, matches + total_found, match_limit - total_found); |
717 | 0 | if (total_found == match_limit) break; |
718 | 0 | if (total_found > 0) { |
719 | 0 | min_length = BackwardMatchLength(&matches[total_found - 1]); |
720 | 0 | } |
721 | 0 | } |
722 | 0 | return total_found; |
723 | 0 | } Unexecuted instantiation: encode.c:LookupAllCompoundDictionaryMatches Unexecuted instantiation: encoder_dict.c:LookupAllCompoundDictionaryMatches Unexecuted instantiation: backward_references_hq.c:LookupAllCompoundDictionaryMatches Unexecuted instantiation: backward_references.c:LookupAllCompoundDictionaryMatches |
724 | | |
725 | | #if defined(__cplusplus) || defined(c_plusplus) |
726 | | } /* extern "C" */ |
727 | | #endif |
728 | | |
729 | | #endif /* BROTLI_ENC_HASH_H_ */ |