Line | Count | Source |
1 | | /* match_tpl.h -- find longest match template for compare256 variants |
2 | | * |
3 | | * Copyright (C) 1995-2024 Jean-loup Gailly and Mark Adler |
4 | | * For conditions of distribution and use, see copyright notice in zlib.h |
5 | | * |
6 | | * Portions copyright (C) 2014-2021 Konstantin Nosov |
7 | | * Fast-zlib optimized longest_match |
8 | | * https://github.com/gildor2/fast_zlib |
9 | | */ |
10 | | |
11 | | #include "insert_string_p.h" |
12 | | |
13 | 0 | #define EARLY_EXIT_TRIGGER_LEVEL 5 |
14 | | |
15 | | #define GOTO_NEXT_CHAIN \ |
16 | 0 | if (--chain_length && (cur_match = prev[cur_match & wmask]) > limit) \ |
17 | 0 | continue; \ |
18 | 0 | return best_len; |
19 | | |
20 | | /* Set match_start to the longest match starting at the given string and |
21 | | * return its length. Matches shorter or equal to prev_length are discarded, |
22 | | * in which case the result is equal to prev_length and match_start is garbage. |
23 | | * |
24 | | * IN assertions: cur_match is the head of the hash chain for the current |
25 | | * string (strstart) and its distance is <= MAX_DIST, and prev_length >=1 |
26 | | * OUT assertion: the match length is not greater than s->lookahead |
27 | | */ |
28 | 0 | Z_INTERNAL uint32_t LONGEST_MATCH(deflate_state *const s, uint32_t cur_match) { |
29 | 0 | const unsigned wmask = W_MASK(s); |
30 | 0 | unsigned int strstart = s->strstart; |
31 | 0 | const unsigned char *window = s->window; |
32 | 0 | const Pos *prev = s->prev; |
33 | | #ifdef LONGEST_MATCH_ROLL |
34 | | const Pos *head = s->head; |
35 | | #endif |
36 | 0 | const unsigned char *scan; |
37 | 0 | const unsigned char *mbase_start = window; |
38 | 0 | const unsigned char *mbase_end; |
39 | 0 | uint32_t limit; |
40 | | #ifdef LONGEST_MATCH_ROLL |
41 | | uint32_t limit_base; |
42 | | #else |
43 | | int32_t early_exit; |
44 | | #endif |
45 | 0 | uint32_t chain_length = s->max_chain_length; |
46 | 0 | uint32_t nice_match = (uint32_t)s->nice_match; |
47 | 0 | uint32_t best_len, offset; |
48 | 0 | uint32_t lookahead = s->lookahead; |
49 | 0 | uint32_t match_offset = 0; |
50 | 0 | uint64_t scan_start; |
51 | 0 | uint64_t scan_end; |
52 | | |
53 | | /* The code is optimized for STD_MAX_MATCH-2 multiple of 16. */ |
54 | 0 | Assert(STD_MAX_MATCH == 258, "Code too clever"); |
55 | |
|
56 | 0 | best_len = s->prev_length ? s->prev_length : STD_MIN_MATCH-1; |
57 | 0 | if (UNLIKELY(best_len >= lookahead)) |
58 | 0 | return lookahead; |
59 | | |
60 | | /* Calculate read offset which should only extend an extra byte to find the |
61 | | * next best match length. When best_len is shorter than the read width, we |
62 | | * diff the mismatched bytes instead. |
63 | | */ |
64 | 0 | offset = best_len >= sizeof(uint64_t) ? best_len - 7 : 0; |
65 | |
|
66 | 0 | scan = window + strstart; |
67 | 0 | scan_start = zng_memread_8(scan); |
68 | 0 | scan_end = zng_memread_8(scan+offset); |
69 | 0 | mbase_end = (mbase_start+offset); |
70 | | |
71 | | /* Do not waste too much time if we already have a good match */ |
72 | 0 | if (UNLIKELY(best_len >= s->good_match)) |
73 | 0 | chain_length >>= 2; |
74 | | |
75 | | /* Stop when cur_match becomes <= limit. To simplify the code, |
76 | | * we prevent matches with the string of window index 0 |
77 | | */ |
78 | 0 | limit = strstart > MAX_DIST(s) ? (strstart - MAX_DIST(s)) : 0; |
79 | | #ifdef LONGEST_MATCH_ROLL |
80 | | limit_base = limit; |
81 | 0 | if (best_len >= STD_MIN_MATCH) { |
82 | | /* We're continuing search (lazy evaluation). */ |
83 | 0 | uint32_t hash; |
84 | 0 | uint32_t pos; |
85 | | |
86 | | /* Find a most distant chain starting from scan with index=1 (index=0 corresponds |
87 | | * to cur_match). We cannot use s->prev[strstart+1,...] immediately, because |
88 | | * these strings are not yet inserted into the hash table. |
89 | | */ |
90 | | // use update_hash_roll for deflate_slow |
91 | | hash = update_hash_roll(0, scan[1]); |
92 | | hash = update_hash_roll(hash, scan[2]); |
93 | | |
94 | 0 | for (uint32_t i = 3; i <= best_len; i++) { |
95 | | // use update_hash_roll for deflate_slow |
96 | 0 | hash = update_hash_roll(hash, scan[i]); |
97 | | /* If we're starting with best_len >= 3, we can use offset search. */ |
98 | 0 | pos = head[hash]; |
99 | 0 | if (UNLIKELY(pos < cur_match)) { |
100 | 0 | match_offset = i - 2; |
101 | 0 | cur_match = pos; |
102 | 0 | } |
103 | 0 | } |
104 | | |
105 | | /* Update offset-dependent variables */ |
106 | 0 | limit = limit_base+match_offset; |
107 | 0 | if (UNLIKELY(cur_match <= limit)) |
108 | 0 | return best_len; |
109 | 0 | mbase_start -= match_offset; |
110 | 0 | mbase_end -= match_offset; |
111 | 0 | } |
112 | | #else |
113 | 0 | early_exit = s->level < EARLY_EXIT_TRIGGER_LEVEL; |
114 | | #endif |
115 | 0 | Assert((unsigned long)strstart <= s->window_size - MIN_LOOKAHEAD, "need lookahead"); |
116 | 0 | for (;;) { |
117 | 0 | if (UNLIKELY(cur_match >= strstart)) |
118 | 0 | break; |
119 | | |
120 | | /* Skip to next match if the match length cannot increase or if the match length is |
121 | | * less than 2. Note that the checks below for insufficient lookahead only occur |
122 | | * occasionally for performance reasons. |
123 | | * Therefore uninitialized memory will be accessed and conditional jumps will be made |
124 | | * that depend on those values. However the length of the match is limited to the |
125 | | * lookahead, so the output of deflate is not affected by the uninitialized values. |
126 | | */ |
127 | 0 | uint32_t len; |
128 | 0 | if (best_len < sizeof(uint64_t)) { |
129 | 0 | uint64_t cand_start = zng_memread_8(mbase_start + cur_match); |
130 | 0 | if (scan_start != cand_start) { |
131 | | /* Peel the first candidate out of the loop. A full 8-byte match falls straight |
132 | | * through to compare256, and single-candidate chains (barely-compressible data) |
133 | | * run with no loop overhead. */ |
134 | 0 | uint64_t first_mask = zng_first_bytes_mask64(best_len + 1); |
135 | 0 | uint64_t diff = scan_start ^ cand_start; |
136 | | /* A candidate beats best_len only when its first best_len+1 bytes match, i.e. |
137 | | * those bytes of the XOR are zero. The masked test rejects without running ctz. */ |
138 | 0 | if (UNLIKELY((diff & first_mask) == 0)) { |
139 | 0 | len = zng_first_diff_byte64(diff); |
140 | 0 | goto short_match_accept; |
141 | 0 | } |
142 | 0 | if (--chain_length == 0 || (cur_match = prev[cur_match & wmask]) <= limit) |
143 | 0 | return best_len; |
144 | 0 | cand_start = zng_memread_8(mbase_start + cur_match); |
145 | 0 | if (scan_start != cand_start) { |
146 | | /* Walk the remaining candidates with the chain advance kept inline. */ |
147 | 0 | for (;;) { |
148 | 0 | diff = scan_start ^ cand_start; |
149 | 0 | if (UNLIKELY((diff & first_mask) == 0)) { |
150 | 0 | len = zng_first_diff_byte64(diff); |
151 | 0 | goto short_match_accept; |
152 | 0 | } |
153 | 0 | if (--chain_length == 0 || (cur_match = prev[cur_match & wmask]) <= limit) |
154 | 0 | return best_len; |
155 | 0 | cand_start = zng_memread_8(mbase_start + cur_match); |
156 | 0 | if (scan_start == cand_start) |
157 | 0 | break; |
158 | 0 | } |
159 | 0 | } |
160 | 0 | } |
161 | | /* All 8 bytes match, fallthrough to compare256 for the tail. */ |
162 | 0 | } else { |
163 | | /* Pre-filter the candidate on the start and end sentinels before compare256 using |
164 | | * simple 8-byte comparison since best_len >= 8. */ |
165 | 0 | for (;;) { |
166 | | /* First check the end of the candidate at best_len+1 due to the higher |
167 | | * likelihood of a mismatch. */ |
168 | 0 | if (zng_memcmp_8(mbase_end+cur_match, &scan_end) == 0 && |
169 | 0 | zng_memcmp_8(mbase_start+cur_match, &scan_start) == 0) |
170 | 0 | break; |
171 | 0 | GOTO_NEXT_CHAIN; |
172 | 0 | } |
173 | 0 | } |
174 | 0 | len = COMPARE256(scan+2, mbase_start+cur_match+2) + 2; |
175 | 0 | Assert(scan+len <= window+(unsigned)(s->window_size-1), "wild scan"); |
176 | |
|
177 | 0 | if (len > best_len) |
178 | 0 | short_match_accept: |
179 | 0 | { |
180 | 0 | uint32_t match_start = cur_match - match_offset; |
181 | 0 | s->match_start = match_start; |
182 | | |
183 | | /* Do not look for better matches if the current match reaches |
184 | | * or exceeds the end of the input. |
185 | | */ |
186 | 0 | if (UNLIKELY(len >= lookahead)) |
187 | 0 | return lookahead; |
188 | 0 | if (UNLIKELY(len >= nice_match)) |
189 | 0 | return len; |
190 | | |
191 | 0 | best_len = len; |
192 | |
|
193 | 0 | offset = best_len >= sizeof(uint64_t) ? best_len - 7 : 0; |
194 | |
|
195 | 0 | scan_end = zng_memread_8(scan+offset); |
196 | |
|
197 | | #ifdef LONGEST_MATCH_ROLL |
198 | | /* Look for a better string offset */ |
199 | 0 | if (UNLIKELY(len > STD_MIN_MATCH && match_start + len < strstart)) { |
200 | 0 | const unsigned char *scan_endstr; |
201 | 0 | uint32_t hash; |
202 | 0 | uint32_t pos, next_pos; |
203 | | |
204 | | /* Go back to offset 0 */ |
205 | | cur_match -= match_offset; |
206 | | match_offset = 0; |
207 | | next_pos = cur_match; |
208 | 0 | for (uint32_t i = 0; i <= len - STD_MIN_MATCH; i++) { |
209 | 0 | pos = prev[(cur_match + i) & wmask]; |
210 | 0 | if (UNLIKELY(pos < next_pos)) { |
211 | | /* Hash chain is more distant, use it */ |
212 | 0 | if (UNLIKELY(pos <= limit_base + i)) |
213 | 0 | return best_len; |
214 | 0 | next_pos = pos; |
215 | 0 | match_offset = i; |
216 | 0 | } |
217 | 0 | } |
218 | | /* Switch cur_match to next_pos chain */ |
219 | 0 | cur_match = next_pos; |
220 | | |
221 | | /* Try hash head at len-(STD_MIN_MATCH-1) position to see if we could get |
222 | | * a better cur_match at the end of string. Using (STD_MIN_MATCH-1) lets |
223 | | * us include one more byte into hash - the byte which will be checked |
224 | | * in main loop now, and which allows to grow match by 1. |
225 | | */ |
226 | 0 | scan_endstr = scan + len - (STD_MIN_MATCH-1); |
227 | |
|
228 | 0 | hash = update_hash_roll(0, scan_endstr[0]); |
229 | 0 | hash = update_hash_roll(hash, scan_endstr[1]); |
230 | 0 | hash = update_hash_roll(hash, scan_endstr[2]); |
231 | |
|
232 | 0 | pos = head[hash]; |
233 | 0 | if (UNLIKELY(pos < cur_match)) { |
234 | 0 | match_offset = len - (STD_MIN_MATCH-1); |
235 | 0 | if (pos <= limit_base + match_offset) |
236 | 0 | return best_len; |
237 | 0 | cur_match = pos; |
238 | 0 | } |
239 | | |
240 | | /* Update offset-dependent variables */ |
241 | 0 | limit = limit_base+match_offset; |
242 | 0 | mbase_start = window-match_offset; |
243 | 0 | mbase_end = (mbase_start+offset); |
244 | 0 | continue; |
245 | 0 | } |
246 | 0 | #endif |
247 | 0 | mbase_end = (mbase_start+offset); |
248 | 0 | } |
249 | | #ifndef LONGEST_MATCH_ROLL |
250 | 0 | else if (UNLIKELY(early_exit)) { |
251 | | /* The probability of finding a match later if we here is pretty low, so for |
252 | | * performance it's best to outright stop here for the lower compression levels |
253 | | */ |
254 | 0 | break; |
255 | 0 | } |
256 | 0 | #endif |
257 | 0 | GOTO_NEXT_CHAIN; |
258 | 0 | } |
259 | 0 | return best_len; |
260 | 0 | } Unexecuted instantiation: longest_match_sse2 Unexecuted instantiation: longest_match_roll_sse2 Unexecuted instantiation: longest_match_avx2 Unexecuted instantiation: longest_match_roll_avx2 Unexecuted instantiation: longest_match_avx512 Unexecuted instantiation: longest_match_roll_avx512 |
261 | | |
262 | | #undef LONGEST_MATCH_ROLL |
263 | | #undef LONGEST_MATCH |