]>
Commit | Line | Data |
---|---|---|
11b7501a SB |
1 | /* NOLINT(build/header_guard) */\r |
2 | /* Copyright 2010 Google Inc. All Rights Reserved.\r | |
3 | \r | |
4 | Distributed under MIT license.\r | |
5 | See file LICENSE for detail or copy at https://opensource.org/licenses/MIT\r | |
6 | */\r | |
7 | \r | |
dd4f667e LG |
8 | /* template parameters: FN, BUCKET_BITS, BUCKET_SWEEP, HASH_LEN,\r |
9 | USE_DICTIONARY\r | |
10 | */\r | |
11b7501a SB |
11 | \r |
12 | #define HashLongestMatchQuickly HASHER()\r | |
13 | \r | |
14 | #define BUCKET_SIZE (1 << BUCKET_BITS)\r | |
15 | \r | |
16 | #define HASH_MAP_SIZE (4 << BUCKET_BITS)\r | |
17 | \r | |
18 | static BROTLI_INLINE size_t FN(HashTypeLength)(void) { return 8; }\r | |
19 | static BROTLI_INLINE size_t FN(StoreLookahead)(void) { return 8; }\r | |
20 | \r | |
21 | /* HashBytes is the function that chooses the bucket to place\r | |
22 | the address in. The HashLongestMatch and HashLongestMatchQuickly\r | |
23 | classes have separate, different implementations of hashing. */\r | |
dd4f667e LG |
24 | static uint32_t FN(HashBytes)(const uint8_t* data) {\r |
25 | const uint64_t h = ((BROTLI_UNALIGNED_LOAD64LE(data) << (64 - 8 * HASH_LEN)) *\r | |
26 | kHashMul64);\r | |
11b7501a SB |
27 | /* The higher bits contain more mixture from the multiplication,\r |
28 | so we take our results from there. */\r | |
29 | return (uint32_t)(h >> (64 - BUCKET_BITS));\r | |
30 | }\r | |
31 | \r | |
32 | /* A (forgetful) hash table to the data seen by the compressor, to\r | |
33 | help create backward references to previous data.\r | |
34 | \r | |
35 | This is a hash map of fixed size (BUCKET_SIZE). Starting from the\r | |
36 | given index, BUCKET_SWEEP buckets are used to store values of a key. */\r | |
37 | typedef struct HashLongestMatchQuickly {\r | |
38 | uint32_t buckets_[BUCKET_SIZE + BUCKET_SWEEP];\r | |
11b7501a SB |
39 | } HashLongestMatchQuickly;\r |
40 | \r | |
dd4f667e LG |
41 | static BROTLI_INLINE HashLongestMatchQuickly* FN(Self)(HasherHandle handle) {\r |
42 | return (HashLongestMatchQuickly*)&(GetHasherCommon(handle)[1]);\r | |
11b7501a SB |
43 | }\r |
44 | \r | |
dd4f667e LG |
45 | static void FN(Initialize)(\r |
46 | HasherHandle handle, const BrotliEncoderParams* params) {\r | |
47 | BROTLI_UNUSED(handle);\r | |
48 | BROTLI_UNUSED(params);\r | |
49 | }\r | |
50 | \r | |
51 | static void FN(Prepare)(HasherHandle handle, BROTLI_BOOL one_shot,\r | |
52 | size_t input_size, const uint8_t* data) {\r | |
53 | HashLongestMatchQuickly* self = FN(Self)(handle);\r | |
54 | /* Partial preparation is 100 times slower (per socket). */\r | |
55 | size_t partial_prepare_threshold = HASH_MAP_SIZE >> 7;\r | |
56 | if (one_shot && input_size <= partial_prepare_threshold) {\r | |
57 | size_t i;\r | |
58 | for (i = 0; i < input_size; ++i) {\r | |
59 | const uint32_t key = FN(HashBytes)(&data[i]);\r | |
60 | memset(&self->buckets_[key], 0, BUCKET_SWEEP * sizeof(self->buckets_[0]));\r | |
61 | }\r | |
62 | } else {\r | |
11b7501a SB |
63 | /* It is not strictly necessary to fill this buffer here, but\r |
64 | not filling will make the results of the compression stochastic\r | |
65 | (but correct). This is because random data would cause the\r | |
66 | system to find accidentally good backward references here and there. */\r | |
67 | memset(&self->buckets_[0], 0, sizeof(self->buckets_));\r | |
11b7501a SB |
68 | }\r |
69 | }\r | |
70 | \r | |
dd4f667e LG |
71 | static BROTLI_INLINE size_t FN(HashMemAllocInBytes)(\r |
72 | const BrotliEncoderParams* params, BROTLI_BOOL one_shot,\r | |
73 | size_t input_size) {\r | |
11b7501a | 74 | BROTLI_UNUSED(params);\r |
dd4f667e LG |
75 | BROTLI_UNUSED(one_shot);\r |
76 | BROTLI_UNUSED(input_size);\r | |
77 | return sizeof(HashLongestMatchQuickly);\r | |
11b7501a SB |
78 | }\r |
79 | \r | |
80 | /* Look at 5 bytes at &data[ix & mask].\r | |
81 | Compute a hash from these, and store the value somewhere within\r | |
82 | [ix .. ix+3]. */\r | |
dd4f667e LG |
83 | static BROTLI_INLINE void FN(Store)(HasherHandle handle,\r |
84 | const uint8_t* data, const size_t mask, const size_t ix) {\r | |
11b7501a SB |
85 | const uint32_t key = FN(HashBytes)(&data[ix & mask]);\r |
86 | /* Wiggle the value with the bucket sweep range. */\r | |
87 | const uint32_t off = (ix >> 3) % BUCKET_SWEEP;\r | |
dd4f667e | 88 | FN(Self)(handle)->buckets_[key + off] = (uint32_t)ix;\r |
11b7501a SB |
89 | }\r |
90 | \r | |
dd4f667e LG |
91 | static BROTLI_INLINE void FN(StoreRange)(HasherHandle handle,\r |
92 | const uint8_t* data, const size_t mask, const size_t ix_start,\r | |
11b7501a SB |
93 | const size_t ix_end) {\r |
94 | size_t i;\r | |
95 | for (i = ix_start; i < ix_end; ++i) {\r | |
dd4f667e | 96 | FN(Store)(handle, data, mask, i);\r |
11b7501a SB |
97 | }\r |
98 | }\r | |
99 | \r | |
100 | static BROTLI_INLINE void FN(StitchToPreviousBlock)(\r | |
dd4f667e | 101 | HasherHandle handle, size_t num_bytes, size_t position,\r |
11b7501a SB |
102 | const uint8_t* ringbuffer, size_t ringbuffer_mask) {\r |
103 | if (num_bytes >= FN(HashTypeLength)() - 1 && position >= 3) {\r | |
104 | /* Prepare the hashes for three last bytes of the last write.\r | |
105 | These could not be calculated before, since they require knowledge\r | |
106 | of both the previous and the current block. */\r | |
dd4f667e LG |
107 | FN(Store)(handle, ringbuffer, ringbuffer_mask, position - 3);\r |
108 | FN(Store)(handle, ringbuffer, ringbuffer_mask, position - 2);\r | |
109 | FN(Store)(handle, ringbuffer, ringbuffer_mask, position - 1);\r | |
11b7501a SB |
110 | }\r |
111 | }\r | |
112 | \r | |
dd4f667e LG |
113 | static BROTLI_INLINE void FN(PrepareDistanceCache)(\r |
114 | HasherHandle handle, int* BROTLI_RESTRICT distance_cache) {\r | |
115 | BROTLI_UNUSED(handle);\r | |
116 | BROTLI_UNUSED(distance_cache);\r | |
117 | }\r | |
118 | \r | |
11b7501a SB |
119 | /* Find a longest backward match of &data[cur_ix & ring_buffer_mask]\r |
120 | up to the length of max_length and stores the position cur_ix in the\r | |
121 | hash table.\r | |
122 | \r | |
123 | Does not look for matches longer than max_length.\r | |
124 | Does not look for matches further away than max_backward.\r | |
125 | Writes the best match into |out|.\r | |
dd4f667e LG |
126 | |out|->score is updated only if a better match is found. */\r |
127 | static BROTLI_INLINE void FN(FindLongestMatch)(\r | |
128 | HasherHandle handle, const BrotliEncoderDictionary* dictionary,\r | |
129 | const uint8_t* BROTLI_RESTRICT data,\r | |
11b7501a SB |
130 | const size_t ring_buffer_mask, const int* BROTLI_RESTRICT distance_cache,\r |
131 | const size_t cur_ix, const size_t max_length, const size_t max_backward,\r | |
dd4f667e | 132 | const size_t gap, const size_t max_distance,\r |
11b7501a | 133 | HasherSearchResult* BROTLI_RESTRICT out) {\r |
dd4f667e | 134 | HashLongestMatchQuickly* self = FN(Self)(handle);\r |
11b7501a SB |
135 | const size_t best_len_in = out->len;\r |
136 | const size_t cur_ix_masked = cur_ix & ring_buffer_mask;\r | |
137 | const uint32_t key = FN(HashBytes)(&data[cur_ix_masked]);\r | |
138 | int compare_char = data[cur_ix_masked + best_len_in];\r | |
dd4f667e | 139 | score_t min_score = out->score;\r |
11b7501a SB |
140 | score_t best_score = out->score;\r |
141 | size_t best_len = best_len_in;\r | |
142 | size_t cached_backward = (size_t)distance_cache[0];\r | |
143 | size_t prev_ix = cur_ix - cached_backward;\r | |
dd4f667e | 144 | out->len_code_delta = 0;\r |
11b7501a SB |
145 | if (prev_ix < cur_ix) {\r |
146 | prev_ix &= (uint32_t)ring_buffer_mask;\r | |
147 | if (compare_char == data[prev_ix + best_len]) {\r | |
148 | size_t len = FindMatchLengthWithLimit(&data[prev_ix],\r | |
149 | &data[cur_ix_masked],\r | |
150 | max_length);\r | |
151 | if (len >= 4) {\r | |
dd4f667e LG |
152 | const score_t score = BackwardReferenceScoreUsingLastDistance(len);\r |
153 | if (best_score < score) {\r | |
154 | best_score = score;\r | |
155 | best_len = len;\r | |
156 | out->len = len;\r | |
157 | out->distance = cached_backward;\r | |
158 | out->score = best_score;\r | |
159 | compare_char = data[cur_ix_masked + best_len];\r | |
160 | if (BUCKET_SWEEP == 1) {\r | |
161 | self->buckets_[key] = (uint32_t)cur_ix;\r | |
162 | return;\r | |
163 | }\r | |
11b7501a SB |
164 | }\r |
165 | }\r | |
166 | }\r | |
167 | }\r | |
168 | if (BUCKET_SWEEP == 1) {\r | |
169 | size_t backward;\r | |
170 | size_t len;\r | |
171 | /* Only one to look for, don't bother to prepare for a loop. */\r | |
172 | prev_ix = self->buckets_[key];\r | |
173 | self->buckets_[key] = (uint32_t)cur_ix;\r | |
174 | backward = cur_ix - prev_ix;\r | |
175 | prev_ix &= (uint32_t)ring_buffer_mask;\r | |
176 | if (compare_char != data[prev_ix + best_len_in]) {\r | |
dd4f667e | 177 | return;\r |
11b7501a | 178 | }\r |
dd4f667e LG |
179 | if (BROTLI_PREDICT_FALSE(backward == 0 || backward > max_backward)) {\r |
180 | return;\r | |
11b7501a SB |
181 | }\r |
182 | len = FindMatchLengthWithLimit(&data[prev_ix],\r | |
183 | &data[cur_ix_masked],\r | |
184 | max_length);\r | |
185 | if (len >= 4) {\r | |
dd4f667e LG |
186 | const score_t score = BackwardReferenceScore(len, backward);\r |
187 | if (best_score < score) {\r | |
188 | out->len = len;\r | |
189 | out->distance = backward;\r | |
190 | out->score = score;\r | |
191 | return;\r | |
192 | }\r | |
11b7501a SB |
193 | }\r |
194 | } else {\r | |
dd4f667e | 195 | uint32_t* bucket = self->buckets_ + key;\r |
11b7501a SB |
196 | int i;\r |
197 | prev_ix = *bucket++;\r | |
198 | for (i = 0; i < BUCKET_SWEEP; ++i, prev_ix = *bucket++) {\r | |
199 | const size_t backward = cur_ix - prev_ix;\r | |
200 | size_t len;\r | |
201 | prev_ix &= (uint32_t)ring_buffer_mask;\r | |
202 | if (compare_char != data[prev_ix + best_len]) {\r | |
203 | continue;\r | |
204 | }\r | |
dd4f667e | 205 | if (BROTLI_PREDICT_FALSE(backward == 0 || backward > max_backward)) {\r |
11b7501a SB |
206 | continue;\r |
207 | }\r | |
208 | len = FindMatchLengthWithLimit(&data[prev_ix],\r | |
209 | &data[cur_ix_masked],\r | |
210 | max_length);\r | |
211 | if (len >= 4) {\r | |
212 | const score_t score = BackwardReferenceScore(len, backward);\r | |
213 | if (best_score < score) {\r | |
214 | best_score = score;\r | |
215 | best_len = len;\r | |
216 | out->len = best_len;\r | |
217 | out->distance = backward;\r | |
218 | out->score = score;\r | |
219 | compare_char = data[cur_ix_masked + best_len];\r | |
11b7501a SB |
220 | }\r |
221 | }\r | |
222 | }\r | |
223 | }\r | |
dd4f667e LG |
224 | if (USE_DICTIONARY && min_score == out->score) {\r |
225 | SearchInStaticDictionary(dictionary,\r | |
226 | handle, &data[cur_ix_masked], max_length, max_backward + gap,\r | |
227 | max_distance, out, BROTLI_TRUE);\r | |
11b7501a SB |
228 | }\r |
229 | self->buckets_[key + ((cur_ix >> 3) % BUCKET_SWEEP)] = (uint32_t)cur_ix;\r | |
11b7501a SB |
230 | }\r |
231 | \r | |
232 | #undef HASH_MAP_SIZE\r | |
233 | #undef BUCKET_SIZE\r | |
234 | \r | |
235 | #undef HashLongestMatchQuickly\r |