Debian Codegraph @

c/enc/hash_longest_match_inc.h

raw symbols SCIP
1 /* NOLINT(build/header_guard) */cxx . . $ `<file>/c/enc/hash_longest_match_inc.h`/
2 /* Copyright 2010 Google Inc. All Rights Reserved.
3
4 Distributed under MIT license.
5 See file LICENSE for detail or copy at https://opensource.org/licenses/MIT
6 */
7
8 /* template parameters: FN */
9
10 /* A (forgetful) hash table to the data seen by the compressor, to
11 help create backward references to previous data.
12
13 This is a hash map of fixed size (bucket_size_) to a ring buffer of
14 fixed size (block_size_). The ring buffer contains the last block_size_
15 index positions of the given hash key in the compressed data. */
16
17 #define HashLongestMatchcxx . . $ `c/enc/hash_longest_match_inc.h:17:9`! HASHERcxx . . $ `c/enc/hash.h:273:9`!()
18
19 static BROTLI_INLINEcxx . . $ `c/common/platform.h:119:9`! size_t FNcxx . . $ `c/enc/hash.h:225:9`!(HashTypeLength)(void) { return 4; }
20 static BROTLI_INLINEcxx . . $ `c/common/platform.h:119:9`! size_t FNcxx . . $ `c/enc/hash.h:225:9`!(StoreLookahead)(void) { return 4; }
21
22 /* HashBytes is the function that chooses the bucket to place the address in. */
23 static uint32_tcxx . . $ uint32_t# FNcxx . . $ `c/enc/hash.h:225:9`!(HashBytes)(
24 const uint8_tcxx . . $ uint8_t#* BROTLI_RESTRICTcxx . . $ `c/common/platform.h:82:9`! data, const int shift) {
25 uint32_tcxx . . $ uint32_t# h = BROTLI_UNALIGNED_LOAD32LEcxx . . $ `c/common/platform.h:379:9`!(data) * kHashMul32cxx . . $ kHashMul32.;
26 /* The higher bits contain more mixture from the multiplication,
27 so we take our results from there. */
28 return (uint32_tcxx . . $ uint32_t#)(h >> shift);
29 }
30
31 typedef struct HashLongestMatchcxx . . $ `c/enc/hash_longest_match_inc.h:17:9`! {
32 /* Number of hash buckets. */
33 size_t bucket_size_cxx . . $ H5#bucket_size_.;
34 /* Only block_size_ newest backward references are kept,
35 and the older are forgotten. */
36 size_t block_size_cxx . . $ H5#block_size_.;
37 /* Left-shift for computing hash bucket index from hash value. */
38 int hash_shift_cxx . . $ H5#hash_shift_.;
39 /* Mask for accessing entries in a block (in a ring-buffer manner). */
40 uint32_tcxx . . $ uint32_t# block_mask_cxx . . $ H5#block_mask_.;
41
42 int block_bits_cxx . . $ H5#block_bits_.;
43 int num_last_distances_to_check_cxx . . $ H5#num_last_distances_to_check_.;
44
45 /* Shortcuts. */
46 HasherCommoncxx . . $ HasherCommon#* common_cxx . . $ H5#common_.;
47
48 /* --- Dynamic size members --- */
49
50 /* Number of entries in a particular bucket. */
51 uint16_tcxx . . $ uint16_t#* num_cxx . . $ H5#num_.; /* uint16_t[bucket_size]; */
52
53 /* Buckets containing block_size_ of backward references. */
54 uint32_tcxx . . $ uint32_t#* buckets_cxx . . $ H5#buckets_.; /* uint32_t[bucket_size * block_size]; */
55 } HashLongestMatchcxx . . $ `c/enc/hash_longest_match_inc.h:17:9`!;
56
57 static void FNcxx . . $ `c/enc/hash.h:225:9`!(Initialize)(
58 HasherCommoncxx . . $ HasherCommon#* common, HashLongestMatchcxx . . $ `c/enc/hash_longest_match_inc.h:17:9`!* BROTLI_RESTRICTcxx . . $ `c/common/platform.h:82:9`! self,
59 const BrotliEncoderParamscxx . . $ BrotliEncoderParams#* params) {
60 self->common_cxx . . $ H5#common_. = common;
61
62 BROTLI_UNUSEDcxx . . $ `c/common/platform.h:514:9`!(params);
63 self->hash_shift_cxx . . $ H5#hash_shift_. = 32 - common->paramscxx . . $ $anonymous_type_5b62deaf4afff4b7_0#params..bucket_bitscxx . . $ BrotliHasherParams#bucket_bits.;
64 self->bucket_size_cxx . . $ H5#bucket_size_. = (size_t)1 << common->paramscxx . . $ $anonymous_type_5b62deaf4afff4b7_0#params..bucket_bitscxx . . $ BrotliHasherParams#bucket_bits.;
65 self->block_size_cxx . . $ H5#block_size_. = (size_t)1 << common->paramscxx . . $ $anonymous_type_5b62deaf4afff4b7_0#params..block_bitscxx . . $ BrotliHasherParams#block_bits.;
66 self->block_mask_cxx . . $ H5#block_mask_. = (uint32_tcxx . . $ uint32_t#)(self->block_size_cxx . . $ H5#block_size_. - 1);
67 self->num_cxx . . $ H5#num_. = (uint16_tcxx . . $ uint16_t#*)common->extracxx . . $ $anonymous_type_5b62deaf4afff4b7_0#extra.[0];
68 self->buckets_cxx . . $ H5#buckets_. = (uint32_tcxx . . $ uint32_t#*)common->extracxx . . $ $anonymous_type_5b62deaf4afff4b7_0#extra.[1];
69 self->block_bits_cxx . . $ H5#block_bits_. = common->paramscxx . . $ $anonymous_type_5b62deaf4afff4b7_0#params..block_bitscxx . . $ BrotliHasherParams#block_bits.;
70 self->num_last_distances_to_check_cxx . . $ H5#num_last_distances_to_check_. =
71 common->paramscxx . . $ $anonymous_type_5b62deaf4afff4b7_0#params..num_last_distances_to_checkcxx . . $ BrotliHasherParams#num_last_distances_to_check.;
72 }
73
74 static void FNcxx . . $ `c/enc/hash.h:225:9`!(Prepare)(
75 HashLongestMatchcxx . . $ `c/enc/hash_longest_match_inc.h:17:9`!* BROTLI_RESTRICTcxx . . $ `c/common/platform.h:82:9`! self, BROTLI_BOOLcxx . . $ `c/include/brotli/types.h:49:9`! one_shot,
76 size_t input_size, const uint8_tcxx . . $ uint8_t#* BROTLI_RESTRICTcxx . . $ `c/common/platform.h:82:9`! data) {
77 uint16_tcxx . . $ uint16_t#* BROTLI_RESTRICTcxx . . $ `c/common/platform.h:82:9`! num = self->num_cxx . . $ H5#num_.;
78 /* Partial preparation is 100 times slower (per socket). */
79 size_t partial_prepare_threshold = self->bucket_size_cxx . . $ H5#bucket_size_. >> 6;
80 if (one_shot && input_size <= partial_prepare_threshold) {
81 size_t i;
82 for (i = 0; i < input_size; ++i) {
83 const uint32_tcxx . . $ uint32_t# key = FNcxx . . $ `c/enc/hash.h:225:9`!(HashBytes)(&data[i], self->hash_shift_cxx . . $ H5#hash_shift_.);
84 num[key] = 0;
85 }
86 } else {
87 memsetcxx . . $ memset(d1de1e4fb215f1c6).(num, 0, self->bucket_size_cxx . . $ H5#bucket_size_. * sizeof(num[0]));
88 }
89 }
90
91 static BROTLI_INLINEcxx . . $ `c/common/platform.h:119:9`! void FNcxx . . $ `c/enc/hash.h:225:9`!(HashMemAllocInBytes)(
92 const BrotliEncoderParamscxx . . $ BrotliEncoderParams#* params, BROTLI_BOOLcxx . . $ `c/include/brotli/types.h:49:9`! one_shot,
93 size_t input_size, size_t* alloc_size) {
94 size_t bucket_size = (size_t)1 << params->hashercxx . . $ BrotliEncoderParams#hasher..bucket_bitscxx . . $ BrotliHasherParams#bucket_bits.;
95 size_t block_size = (size_t)1 << params->hashercxx . . $ BrotliEncoderParams#hasher..block_bitscxx . . $ BrotliHasherParams#block_bits.;
96 BROTLI_UNUSEDcxx . . $ `c/common/platform.h:514:9`!(one_shot);
97 BROTLI_UNUSEDcxx . . $ `c/common/platform.h:514:9`!(input_size);
98 alloc_size[0] = sizeof(uint16_tcxx . . $ uint16_t#) * bucket_size;
99 alloc_size[1] = sizeof(uint32_tcxx . . $ uint32_t#) * bucket_size * block_size;
100 }
101
102 /* Look at 4 bytes at &data[ix & mask].
103 Compute a hash from these, and store the value of ix at that position. */
104 static BROTLI_INLINEcxx . . $ `c/common/platform.h:119:9`! void FNcxx . . $ `c/enc/hash.h:225:9`!(Store)(
105 HashLongestMatchcxx . . $ `c/enc/hash_longest_match_inc.h:17:9`!* BROTLI_RESTRICTcxx . . $ `c/common/platform.h:82:9`! self, const uint8_tcxx . . $ uint8_t#* BROTLI_RESTRICTcxx . . $ `c/common/platform.h:82:9`! data,
106 const size_t mask, const size_t ix) {
107 uint16_tcxx . . $ uint16_t#* BROTLI_RESTRICTcxx . . $ `c/common/platform.h:82:9`! num = self->num_cxx . . $ H5#num_.;
108 uint32_tcxx . . $ uint32_t#* BROTLI_RESTRICTcxx . . $ `c/common/platform.h:82:9`! buckets = self->buckets_cxx . . $ H5#buckets_.;
109 const uint32_tcxx . . $ uint32_t# key = FNcxx . . $ `c/enc/hash.h:225:9`!(HashBytes)(&data[ix & mask], self->hash_shift_cxx . . $ H5#hash_shift_.);
110 const size_t minor_ix = num[key] & self->block_mask_cxx . . $ H5#block_mask_.;
111 const size_t offset = minor_ix + (key << self->block_bits_cxx . . $ H5#block_bits_.);
112 ++num[key];
113 buckets[offset] = (uint32_tcxx . . $ uint32_t#)ix;
114 }
115
116 static BROTLI_INLINEcxx . . $ `c/common/platform.h:119:9`! void FNcxx . . $ `c/enc/hash.h:225:9`!(StoreRange)(HashLongestMatchcxx . . $ `c/enc/hash_longest_match_inc.h:17:9`!* BROTLI_RESTRICTcxx . . $ `c/common/platform.h:82:9`! self,
117 const uint8_tcxx . . $ uint8_t#* BROTLI_RESTRICTcxx . . $ `c/common/platform.h:82:9`! data, const size_t mask,
118 const size_t ix_start, const size_t ix_end) {
119 size_t i;
120 for (i = ix_start; i < ix_end; ++i) {
121 FNcxx . . $ `c/enc/hash.h:225:9`!(Store)(self, data, mask, i);
122 }
123 }
124
125 static BROTLI_INLINEcxx . . $ `c/common/platform.h:119:9`! void FNcxx . . $ `c/enc/hash.h:225:9`!(StitchToPreviousBlock)(
126 HashLongestMatchcxx . . $ `c/enc/hash_longest_match_inc.h:17:9`!* BROTLI_RESTRICTcxx . . $ `c/common/platform.h:82:9`! self,
127 size_t num_bytes, size_t position, const uint8_tcxx . . $ uint8_t#* ringbuffer,
128 size_t ringbuffer_mask) {
129 if (num_bytes >= FNcxx . . $ `c/enc/hash.h:225:9`!(HashTypeLength)() - 1 && position >= 3) {
130 /* Prepare the hashes for three last bytes of the last write.
131 These could not be calculated before, since they require knowledge
132 of both the previous and the current block. */
133 FNcxx . . $ `c/enc/hash.h:225:9`!(Store)(self, ringbuffer, ringbuffer_mask, position - 3);
134 FNcxx . . $ `c/enc/hash.h:225:9`!(Store)(self, ringbuffer, ringbuffer_mask, position - 2);
135 FNcxx . . $ `c/enc/hash.h:225:9`!(Store)(self, ringbuffer, ringbuffer_mask, position - 1);
136 }
137 }
138
139 static BROTLI_INLINEcxx . . $ `c/common/platform.h:119:9`! void FNcxx . . $ `c/enc/hash.h:225:9`!(PrepareDistanceCache)(
140 HashLongestMatchcxx . . $ `c/enc/hash_longest_match_inc.h:17:9`!* BROTLI_RESTRICTcxx . . $ `c/common/platform.h:82:9`! self,
141 int* BROTLI_RESTRICTcxx . . $ `c/common/platform.h:82:9`! distance_cache) {
142 PrepareDistanceCachecxx . . $ PrepareDistanceCache(621ccea12ecf5b06).(distance_cache, self->num_last_distances_to_check_cxx . . $ H5#num_last_distances_to_check_.);
143 }
144
145 /* Find a longest backward match of &data[cur_ix] up to the length of
146 max_length and stores the position cur_ix in the hash table.
147
148 REQUIRES: FN(PrepareDistanceCache) must be invoked for current distance cache
149 values; if this method is invoked repeatedly with the same distance
150 cache values, it is enough to invoke FN(PrepareDistanceCache) once.
151
152 Does not look for matches longer than max_length.
153 Does not look for matches further away than max_backward.
154 Writes the best match into |out|.
155 |out|->score is updated only if a better match is found. */
156 static BROTLI_INLINEcxx . . $ `c/common/platform.h:119:9`! void FNcxx . . $ `c/enc/hash.h:225:9`!(FindLongestMatch)(
157 HashLongestMatchcxx . . $ `c/enc/hash_longest_match_inc.h:17:9`!* BROTLI_RESTRICTcxx . . $ `c/common/platform.h:82:9`! self,
158 const BrotliEncoderDictionarycxx . . $ BrotliEncoderDictionary#* dictionary,
159 const uint8_tcxx . . $ uint8_t#* BROTLI_RESTRICTcxx . . $ `c/common/platform.h:82:9`! data, const size_t ring_buffer_mask,
160 const int* BROTLI_RESTRICTcxx . . $ `c/common/platform.h:82:9`! distance_cache, const size_t cur_ix,
161 const size_t max_length, const size_t max_backward,
162 const size_t dictionary_distance, const size_t max_distance,
163 HasherSearchResultcxx . . $ HasherSearchResult#* BROTLI_RESTRICTcxx . . $ `c/common/platform.h:82:9`! out) {
164 uint16_tcxx . . $ uint16_t#* BROTLI_RESTRICTcxx . . $ `c/common/platform.h:82:9`! num = self->num_cxx . . $ H5#num_.;
165 uint32_tcxx . . $ uint32_t#* BROTLI_RESTRICTcxx . . $ `c/common/platform.h:82:9`! buckets = self->buckets_cxx . . $ H5#buckets_.;
166 const size_t cur_ix_masked = cur_ix & ring_buffer_mask;
167 /* Don't accept a short copy from far away. */
168 score_tcxx . . $ `c/enc/hash.h:57:9`! min_score = out->scorecxx . . $ HasherSearchResult#score.;
169 score_tcxx . . $ `c/enc/hash.h:57:9`! best_score = out->scorecxx . . $ HasherSearchResult#score.;
170 size_t best_len = out->lencxx . . $ HasherSearchResult#len.;
171 size_t i;
172 /* Precalculate the hash key and prefetch the bucket. */
173 const uint32_tcxx . . $ uint32_t# key =
174 FNcxx . . $ `c/enc/hash.h:225:9`!(HashBytes)(&data[cur_ix_masked], self->hash_shift_cxx . . $ H5#hash_shift_.);
175 uint32_tcxx . . $ uint32_t#* BROTLI_RESTRICTcxx . . $ `c/common/platform.h:82:9`! bucket = &buckets[key << self->block_bits_cxx . . $ H5#block_bits_.];
176 PREFETCH_L1cxx . . $ `c/common/platform.h:627:9`!(bucket);
177 if (self->block_bits_cxx . . $ H5#block_bits_. > 4) PREFETCH_L1cxx . . $ `c/common/platform.h:627:9`!(bucket + 16);
178 out->lencxx . . $ HasherSearchResult#len. = 0;
179 out->len_code_deltacxx . . $ HasherSearchResult#len_code_delta. = 0;
180
181 BROTLI_DCHECKcxx . . $ `c/common/platform.h:473:9`!(cur_ix_masked + max_length <= ring_buffer_mask);
182
183 /* Try last distance first. */
184 for (i = 0; i < (size_t)self->num_last_distances_to_check_cxx . . $ H5#num_last_distances_to_check_.; ++i) {
185 const size_t backward = (size_t)distance_cache[i];
186 size_t prev_ix = (size_t)(cur_ix - backward);
187 if (prev_ix >= cur_ix) {
188 continue;
189 }
190 if (BROTLI_PREDICT_FALSEcxx . . $ `c/common/platform.h:74:9`!(backward > max_backward)) {
191 continue;
192 }
193 prev_ix &= ring_buffer_mask;
194
195 if (cur_ix_masked + best_len > ring_buffer_mask) {
196 break;
197 }
198 if (prev_ix + best_len > ring_buffer_mask ||
199 data[cur_ix_masked + best_len] != data[prev_ix + best_len]) {
200 continue;
201 }
202 {
203 const size_t len = FindMatchLengthWithLimitcxx . . $ FindMatchLengthWithLimit(c2ccccc71581acc8).(&data[prev_ix],
204 &data[cur_ix_masked],
205 max_length);
206 if (len >= 3 || (len == 2 && i < 2)) {
207 /* Comparing for >= 2 does not change the semantics, but just saves for
208 a few unnecessary binary logarithms in backward reference score,
209 since we are not interested in such short matches. */
210 score_tcxx . . $ `c/enc/hash.h:57:9`! score = BackwardReferenceScoreUsingLastDistancecxx . . $ BackwardReferenceScoreUsingLastDistance(ecdaa1632f49f591).(len);
211 if (best_score < score) {
212 if (i != 0) score -= BackwardReferencePenaltyUsingLastDistancecxx . . $ BackwardReferencePenaltyUsingLastDistance(ecdaa1632f49f591).(i);
213 if (best_score < score) {
214 best_score = score;
215 best_len = len;
216 out->lencxx . . $ HasherSearchResult#len. = best_len;
217 out->distancecxx . . $ HasherSearchResult#distance. = backward;
218 out->scorecxx . . $ HasherSearchResult#score. = best_score;
219 }
220 }
221 }
222 }
223 }
224 /* we require matches of len >4, so increase best_len to 3, so we can compare
225 * 4 bytes all the time. */
226 if (best_len < 3) {
227 best_len = 3;
228 }
229 {
230 const size_t down =
231 (num[key] > self->block_size_cxx . . $ H5#block_size_.) ? (num[key] - self->block_size_cxx . . $ H5#block_size_.) : 0u;
232 for (i = num[key]; i > down;) {
233 size_t prev_ix = bucket[--i & self->block_mask_cxx . . $ H5#block_mask_.];
234 const size_t backward = cur_ix - prev_ix;
235 if (BROTLI_PREDICT_FALSEcxx . . $ `c/common/platform.h:74:9`!(backward > max_backward)) {
236 break;
237 }
238 prev_ix &= ring_buffer_mask;
239 if (cur_ix_masked + best_len > ring_buffer_mask) {
240 break;
241 }
242 if (prev_ix + best_len > ring_buffer_mask ||
243 /* compare 4 bytes ending at best_len + 1 */
244 BrotliUnalignedRead32cxx . . $ BrotliUnalignedRead32(b11f534102b1ad6).(&data[cur_ix_masked + best_len - 3]) !=
245 BrotliUnalignedRead32cxx . . $ BrotliUnalignedRead32(b11f534102b1ad6).(&data[prev_ix + best_len - 3])) {
246 continue;
247 }
248 {
249 const size_t len = FindMatchLengthWithLimitcxx . . $ FindMatchLengthWithLimit(c2ccccc71581acc8).(&data[prev_ix],
250 &data[cur_ix_masked],
251 max_length);
252 if (len >= 4) {
253 /* Comparing for >= 3 does not change the semantics, but just saves
254 for a few unnecessary binary logarithms in backward reference
255 score, since we are not interested in such short matches. */
256 score_tcxx . . $ `c/enc/hash.h:57:9`! score = BackwardReferenceScorecxx . . $ BackwardReferenceScore(80bad76eed2d2579).(len, backward);
257 if (best_score < score) {
258 best_score = score;
259 best_len = len;
260 out->lencxx . . $ HasherSearchResult#len. = best_len;
261 out->distancecxx . . $ HasherSearchResult#distance. = backward;
262 out->scorecxx . . $ HasherSearchResult#score. = best_score;
263 }
264 }
265 }
266 }
267 bucket[num[key] & self->block_mask_cxx . . $ H5#block_mask_.] = (uint32_tcxx . . $ uint32_t#)cur_ix;
268 ++num[key];
269 }
270 if (min_score == out->scorecxx . . $ HasherSearchResult#score.) {
271 SearchInStaticDictionarycxx . . $ SearchInStaticDictionary(e21d390e7ad53fe0).(dictionary,
272 self->common_cxx . . $ H5#common_., &data[cur_ix_masked], max_length, dictionary_distance,
273 max_distance, out, BROTLI_FALSEcxx . . $ `c/include/brotli/types.h:53:9`!);
274 }
275 }
276
277 #undef HashLongestMatchcxx . . $ `c/enc/hash_longest_match_inc.h:17:9`!