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 | 254M | #define EARLY_EXIT_TRIGGER_LEVEL 5 |
14 | | |
15 | | #define GOTO_NEXT_CHAIN \ |
16 | 204M | if (--chain_length && (cur_match = prev[cur_match & wmask]) > limit) \ |
17 | 194M | continue; \ |
18 | 10.3M | 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 | 291M | Z_INTERNAL uint32_t LONGEST_MATCH(deflate_state *const s, uint32_t cur_match) { |
29 | 291M | const unsigned wmask = W_MASK(s); |
30 | 291M | unsigned int strstart = s->strstart; |
31 | 291M | const unsigned char *window = s->window; |
32 | 291M | const Pos *prev = s->prev; |
33 | | #ifdef LONGEST_MATCH_ROLL |
34 | | const Pos *head = s->head; |
35 | | #endif |
36 | 291M | const unsigned char *scan; |
37 | 291M | const unsigned char *mbase_start = window; |
38 | 291M | const unsigned char *mbase_end; |
39 | 291M | uint32_t limit; |
40 | | #ifdef LONGEST_MATCH_ROLL |
41 | | uint32_t limit_base; |
42 | | #else |
43 | | int32_t early_exit; |
44 | | #endif |
45 | 291M | uint32_t chain_length = s->max_chain_length; |
46 | 291M | uint32_t nice_match = (uint32_t)s->nice_match; |
47 | 291M | uint32_t best_len, offset; |
48 | 291M | uint32_t lookahead = s->lookahead; |
49 | 291M | uint32_t match_offset = 0; |
50 | 291M | uint64_t scan_start; |
51 | 291M | uint64_t scan_end; |
52 | | |
53 | | /* The code is optimized for STD_MAX_MATCH-2 multiple of 16. */ |
54 | 291M | Assert(STD_MAX_MATCH == 258, "Code too clever"); |
55 | | |
56 | 291M | best_len = s->prev_length ? s->prev_length : STD_MIN_MATCH-1; |
57 | 291M | if (UNLIKELY(best_len >= lookahead)) |
58 | 5.57k | 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 | 291M | offset = best_len >= sizeof(uint64_t) ? best_len - 7 : 0; |
65 | | |
66 | 291M | scan = window + strstart; |
67 | 291M | scan_start = zng_memread_8(scan); |
68 | 291M | scan_end = zng_memread_8(scan+offset); |
69 | 291M | mbase_end = (mbase_start+offset); |
70 | | |
71 | | /* Do not waste too much time if we already have a good match */ |
72 | 291M | if (UNLIKELY(best_len >= s->good_match)) |
73 | 729k | 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 | 291M | limit = strstart > MAX_DIST(s) ? (strstart - MAX_DIST(s)) : 0; |
79 | | #ifdef LONGEST_MATCH_ROLL |
80 | | limit_base = limit; |
81 | 36.8M | if (best_len >= STD_MIN_MATCH) { |
82 | | /* We're continuing search (lazy evaluation). */ |
83 | 6.60M | uint32_t hash; |
84 | 6.60M | 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 | 28.8M | for (uint32_t i = 3; i <= best_len; i++) { |
95 | | // use update_hash_roll for deflate_slow |
96 | 22.1M | hash = update_hash_roll(hash, scan[i]); |
97 | | /* If we're starting with best_len >= 3, we can use offset search. */ |
98 | 22.1M | pos = head[hash]; |
99 | 22.1M | if (UNLIKELY(pos < cur_match)) { |
100 | 5.41M | match_offset = i - 2; |
101 | 5.41M | cur_match = pos; |
102 | 5.41M | } |
103 | 22.1M | } |
104 | | |
105 | | /* Update offset-dependent variables */ |
106 | 6.60M | limit = limit_base+match_offset; |
107 | 6.60M | if (UNLIKELY(cur_match <= limit)) |
108 | 1.57M | return best_len; |
109 | 5.03M | mbase_start -= match_offset; |
110 | 5.03M | mbase_end -= match_offset; |
111 | 5.03M | } |
112 | | #else |
113 | 254M | early_exit = s->level < EARLY_EXIT_TRIGGER_LEVEL; |
114 | | #endif |
115 | 35.2M | Assert((unsigned long)strstart <= s->window_size - MIN_LOOKAHEAD, "need lookahead"); |
116 | 328M | for (;;) { |
117 | 328M | if (UNLIKELY(cur_match >= strstart)) |
118 | 82 | 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 | 328M | uint32_t len; |
128 | 328M | if (best_len < sizeof(uint64_t)) { |
129 | 312M | uint64_t cand_start = zng_memread_8(mbase_start + cur_match); |
130 | 312M | 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 | 306M | uint64_t first_mask = zng_first_bytes_mask64(best_len + 1); |
135 | 306M | 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 | 306M | if (UNLIKELY((diff & first_mask) == 0)) { |
139 | 26.6M | len = zng_first_diff_byte64(diff); |
140 | 26.6M | goto short_match_accept; |
141 | 26.6M | } |
142 | 279M | if (--chain_length == 0 || (cur_match = prev[cur_match & wmask]) <= limit) |
143 | 195M | return best_len; |
144 | 84.4M | cand_start = zng_memread_8(mbase_start + cur_match); |
145 | 84.4M | if (scan_start != cand_start) { |
146 | | /* Walk the remaining candidates with the chain advance kept inline. */ |
147 | 363M | for (;;) { |
148 | 363M | diff = scan_start ^ cand_start; |
149 | 363M | if (UNLIKELY((diff & first_mask) == 0)) { |
150 | 5.08M | len = zng_first_diff_byte64(diff); |
151 | 5.08M | goto short_match_accept; |
152 | 5.08M | } |
153 | 357M | if (--chain_length == 0 || (cur_match = prev[cur_match & wmask]) <= limit) |
154 | 78.5M | return best_len; |
155 | 279M | cand_start = zng_memread_8(mbase_start + cur_match); |
156 | 279M | if (scan_start == cand_start) |
157 | 599k | break; |
158 | 279M | } |
159 | 84.1M | } |
160 | 84.4M | } |
161 | | /* All 8 bytes match, fallthrough to compare256 for the tail. */ |
162 | 312M | } 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 | 175M | for (;;) { |
166 | | /* First check the end of the candidate at best_len+1 due to the higher |
167 | | * likelihood of a mismatch. */ |
168 | 175M | if (zng_memcmp_8(mbase_end+cur_match, &scan_end) == 0 && |
169 | 14.8M | zng_memcmp_8(mbase_start+cur_match, &scan_start) == 0) |
170 | 12.4M | break; |
171 | 162M | GOTO_NEXT_CHAIN; |
172 | 162M | } |
173 | 15.8M | } |
174 | 19.8M | len = COMPARE256(scan+2, mbase_start+cur_match+2) + 2; |
175 | 19.8M | Assert(scan+len <= window+(unsigned)(s->window_size-1), "wild scan"); |
176 | | |
177 | 19.8M | if (len > best_len) |
178 | 63.7M | short_match_accept: |
179 | 63.7M | { |
180 | 63.7M | uint32_t match_start = cur_match - match_offset; |
181 | 63.7M | 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 | 63.7M | if (UNLIKELY(len >= lookahead)) |
187 | 12.3k | return lookahead; |
188 | 47.7M | if (UNLIKELY(len >= nice_match)) |
189 | 3.57M | return len; |
190 | | |
191 | 44.1M | best_len = len; |
192 | | |
193 | 44.1M | offset = best_len >= sizeof(uint64_t) ? best_len - 7 : 0; |
194 | | |
195 | 44.1M | scan_end = zng_memread_8(scan+offset); |
196 | | |
197 | | #ifdef LONGEST_MATCH_ROLL |
198 | | /* Look for a better string offset */ |
199 | 12.6M | if (UNLIKELY(len > STD_MIN_MATCH && match_start + len < strstart)) { |
200 | 6.22M | const unsigned char *scan_endstr; |
201 | 6.22M | uint32_t hash; |
202 | 6.22M | 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 | 158M | for (uint32_t i = 0; i <= len - STD_MIN_MATCH; i++) { |
209 | 153M | pos = prev[(cur_match + i) & wmask]; |
210 | 153M | if (UNLIKELY(pos < next_pos)) { |
211 | | /* Hash chain is more distant, use it */ |
212 | 8.05M | if (UNLIKELY(pos <= limit_base + i)) |
213 | 1.42M | return best_len; |
214 | 6.62M | next_pos = pos; |
215 | 6.62M | match_offset = i; |
216 | 6.62M | } |
217 | 153M | } |
218 | | /* Switch cur_match to next_pos chain */ |
219 | 4.79M | 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 | 4.79M | scan_endstr = scan + len - (STD_MIN_MATCH-1); |
227 | | |
228 | 4.79M | hash = update_hash_roll(0, scan_endstr[0]); |
229 | 4.79M | hash = update_hash_roll(hash, scan_endstr[1]); |
230 | 4.79M | hash = update_hash_roll(hash, scan_endstr[2]); |
231 | | |
232 | 4.79M | pos = head[hash]; |
233 | 4.79M | if (UNLIKELY(pos < cur_match)) { |
234 | 855k | match_offset = len - (STD_MIN_MATCH-1); |
235 | 855k | if (pos <= limit_base + match_offset) |
236 | 408k | return best_len; |
237 | 446k | cur_match = pos; |
238 | 446k | } |
239 | | |
240 | | /* Update offset-dependent variables */ |
241 | 4.38M | limit = limit_base+match_offset; |
242 | 4.38M | mbase_start = window-match_offset; |
243 | 4.38M | mbase_end = (mbase_start+offset); |
244 | 4.38M | continue; |
245 | 4.79M | } |
246 | 6.45M | #endif |
247 | 6.45M | mbase_end = (mbase_start+offset); |
248 | 6.45M | } |
249 | | #ifndef LONGEST_MATCH_ROLL |
250 | 1.22M | 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 | 795 | break; |
255 | 795 | } |
256 | 32.7M | #endif |
257 | 41.7M | GOTO_NEXT_CHAIN; |
258 | 41.7M | } |
259 | 877 | return best_len; |
260 | 35.2M | } Unexecuted instantiation: longest_match_sse2 Unexecuted instantiation: longest_match_roll_sse2 Line | Count | Source | 28 | 254M | Z_INTERNAL uint32_t LONGEST_MATCH(deflate_state *const s, uint32_t cur_match) { | 29 | 254M | const unsigned wmask = W_MASK(s); | 30 | 254M | unsigned int strstart = s->strstart; | 31 | 254M | const unsigned char *window = s->window; | 32 | 254M | const Pos *prev = s->prev; | 33 | | #ifdef LONGEST_MATCH_ROLL | 34 | | const Pos *head = s->head; | 35 | | #endif | 36 | 254M | const unsigned char *scan; | 37 | 254M | const unsigned char *mbase_start = window; | 38 | 254M | const unsigned char *mbase_end; | 39 | 254M | uint32_t limit; | 40 | | #ifdef LONGEST_MATCH_ROLL | 41 | | uint32_t limit_base; | 42 | | #else | 43 | 254M | int32_t early_exit; | 44 | 254M | #endif | 45 | 254M | uint32_t chain_length = s->max_chain_length; | 46 | 254M | uint32_t nice_match = (uint32_t)s->nice_match; | 47 | 254M | uint32_t best_len, offset; | 48 | 254M | uint32_t lookahead = s->lookahead; | 49 | 254M | uint32_t match_offset = 0; | 50 | 254M | uint64_t scan_start; | 51 | 254M | uint64_t scan_end; | 52 | | | 53 | | /* The code is optimized for STD_MAX_MATCH-2 multiple of 16. */ | 54 | 254M | Assert(STD_MAX_MATCH == 258, "Code too clever"); | 55 | | | 56 | 254M | best_len = s->prev_length ? s->prev_length : STD_MIN_MATCH-1; | 57 | 254M | if (UNLIKELY(best_len >= lookahead)) | 58 | 1.40k | 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 | 254M | offset = best_len >= sizeof(uint64_t) ? best_len - 7 : 0; | 65 | | | 66 | 254M | scan = window + strstart; | 67 | 254M | scan_start = zng_memread_8(scan); | 68 | 254M | scan_end = zng_memread_8(scan+offset); | 69 | 254M | mbase_end = (mbase_start+offset); | 70 | | | 71 | | /* Do not waste too much time if we already have a good match */ | 72 | 254M | if (UNLIKELY(best_len >= s->good_match)) | 73 | 634k | 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 | 254M | limit = strstart > MAX_DIST(s) ? (strstart - MAX_DIST(s)) : 0; | 79 | | #ifdef LONGEST_MATCH_ROLL | 80 | | limit_base = limit; | 81 | | if (best_len >= STD_MIN_MATCH) { | 82 | | /* We're continuing search (lazy evaluation). */ | 83 | | uint32_t hash; | 84 | | 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 | | for (uint32_t i = 3; i <= best_len; i++) { | 95 | | // use update_hash_roll for deflate_slow | 96 | | hash = update_hash_roll(hash, scan[i]); | 97 | | /* If we're starting with best_len >= 3, we can use offset search. */ | 98 | | pos = head[hash]; | 99 | | if (UNLIKELY(pos < cur_match)) { | 100 | | match_offset = i - 2; | 101 | | cur_match = pos; | 102 | | } | 103 | | } | 104 | | | 105 | | /* Update offset-dependent variables */ | 106 | | limit = limit_base+match_offset; | 107 | | if (UNLIKELY(cur_match <= limit)) | 108 | | return best_len; | 109 | | mbase_start -= match_offset; | 110 | | mbase_end -= match_offset; | 111 | | } | 112 | | #else | 113 | 254M | early_exit = s->level < EARLY_EXIT_TRIGGER_LEVEL; | 114 | 254M | #endif | 115 | 254M | Assert((unsigned long)strstart <= s->window_size - MIN_LOOKAHEAD, "need lookahead"); | 116 | 280M | for (;;) { | 117 | 280M | if (UNLIKELY(cur_match >= strstart)) | 118 | 82 | 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 | 280M | uint32_t len; | 128 | 280M | if (best_len < sizeof(uint64_t)) { | 129 | 269M | uint64_t cand_start = zng_memread_8(mbase_start + cur_match); | 130 | 269M | 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 | 265M | uint64_t first_mask = zng_first_bytes_mask64(best_len + 1); | 135 | 265M | 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 | 265M | if (UNLIKELY((diff & first_mask) == 0)) { | 139 | 19.5M | len = zng_first_diff_byte64(diff); | 140 | 19.5M | goto short_match_accept; | 141 | 19.5M | } | 142 | 245M | if (--chain_length == 0 || (cur_match = prev[cur_match & wmask]) <= limit) | 143 | 181M | return best_len; | 144 | 63.8M | cand_start = zng_memread_8(mbase_start + cur_match); | 145 | 63.8M | if (scan_start != cand_start) { | 146 | | /* Walk the remaining candidates with the chain advance kept inline. */ | 147 | 218M | for (;;) { | 148 | 218M | diff = scan_start ^ cand_start; | 149 | 218M | if (UNLIKELY((diff & first_mask) == 0)) { | 150 | 1.76M | len = zng_first_diff_byte64(diff); | 151 | 1.76M | goto short_match_accept; | 152 | 1.76M | } | 153 | 216M | if (--chain_length == 0 || (cur_match = prev[cur_match & wmask]) <= limit) | 154 | 61.2M | return best_len; | 155 | 155M | cand_start = zng_memread_8(mbase_start + cur_match); | 156 | 155M | if (scan_start == cand_start) | 157 | 537k | break; | 158 | 155M | } | 159 | 63.5M | } | 160 | 63.8M | } | 161 | | /* All 8 bytes match, fallthrough to compare256 for the tail. */ | 162 | 269M | } 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 | 118M | for (;;) { | 166 | | /* First check the end of the candidate at best_len+1 due to the higher | 167 | | * likelihood of a mismatch. */ | 168 | 118M | if (zng_memcmp_8(mbase_end+cur_match, &scan_end) == 0 && | 169 | 8.70M | zng_memcmp_8(mbase_start+cur_match, &scan_start) == 0) | 170 | 8.02M | break; | 171 | 110M | GOTO_NEXT_CHAIN; | 172 | 110M | } | 173 | 11.0M | } | 174 | 13.2M | len = COMPARE256(scan+2, mbase_start+cur_match+2) + 2; | 175 | 13.2M | Assert(scan+len <= window+(unsigned)(s->window_size-1), "wild scan"); | 176 | | | 177 | 13.2M | if (len > best_len) | 178 | 45.4M | short_match_accept: | 179 | 45.4M | { | 180 | 45.4M | uint32_t match_start = cur_match - match_offset; | 181 | 45.4M | 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 | 45.4M | if (UNLIKELY(len >= lookahead)) | 187 | 7.98k | return lookahead; | 188 | 33.3M | if (UNLIKELY(len >= nice_match)) | 189 | 1.83M | return len; | 190 | | | 191 | 31.5M | best_len = len; | 192 | | | 193 | 31.5M | offset = best_len >= sizeof(uint64_t) ? best_len - 7 : 0; | 194 | | | 195 | 31.5M | scan_end = zng_memread_8(scan+offset); | 196 | | | 197 | | #ifdef LONGEST_MATCH_ROLL | 198 | | /* Look for a better string offset */ | 199 | | if (UNLIKELY(len > STD_MIN_MATCH && match_start + len < strstart)) { | 200 | | const unsigned char *scan_endstr; | 201 | | uint32_t hash; | 202 | | 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 | | for (uint32_t i = 0; i <= len - STD_MIN_MATCH; i++) { | 209 | | pos = prev[(cur_match + i) & wmask]; | 210 | | if (UNLIKELY(pos < next_pos)) { | 211 | | /* Hash chain is more distant, use it */ | 212 | | if (UNLIKELY(pos <= limit_base + i)) | 213 | | return best_len; | 214 | | next_pos = pos; | 215 | | match_offset = i; | 216 | | } | 217 | | } | 218 | | /* Switch cur_match to next_pos chain */ | 219 | | 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 | | scan_endstr = scan + len - (STD_MIN_MATCH-1); | 227 | | | 228 | | hash = update_hash_roll(0, scan_endstr[0]); | 229 | | hash = update_hash_roll(hash, scan_endstr[1]); | 230 | | hash = update_hash_roll(hash, scan_endstr[2]); | 231 | | | 232 | | pos = head[hash]; | 233 | | if (UNLIKELY(pos < cur_match)) { | 234 | | match_offset = len - (STD_MIN_MATCH-1); | 235 | | if (pos <= limit_base + match_offset) | 236 | | return best_len; | 237 | | cur_match = pos; | 238 | | } | 239 | | | 240 | | /* Update offset-dependent variables */ | 241 | | limit = limit_base+match_offset; | 242 | | mbase_start = window-match_offset; | 243 | | mbase_end = (mbase_start+offset); | 244 | | continue; | 245 | | } | 246 | | #endif | 247 | 31.5M | mbase_end = (mbase_start+offset); | 248 | 31.5M | } | 249 | 1.22M | #ifndef LONGEST_MATCH_ROLL | 250 | 1.22M | 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 | 795 | break; | 255 | 795 | } | 256 | 32.7M | #endif | 257 | 32.7M | GOTO_NEXT_CHAIN; | 258 | 32.7M | } | 259 | 877 | return best_len; | 260 | 254M | } |
Line | Count | Source | 28 | 36.8M | Z_INTERNAL uint32_t LONGEST_MATCH(deflate_state *const s, uint32_t cur_match) { | 29 | 36.8M | const unsigned wmask = W_MASK(s); | 30 | 36.8M | unsigned int strstart = s->strstart; | 31 | 36.8M | const unsigned char *window = s->window; | 32 | 36.8M | const Pos *prev = s->prev; | 33 | 36.8M | #ifdef LONGEST_MATCH_ROLL | 34 | 36.8M | const Pos *head = s->head; | 35 | 36.8M | #endif | 36 | 36.8M | const unsigned char *scan; | 37 | 36.8M | const unsigned char *mbase_start = window; | 38 | 36.8M | const unsigned char *mbase_end; | 39 | 36.8M | uint32_t limit; | 40 | 36.8M | #ifdef LONGEST_MATCH_ROLL | 41 | 36.8M | uint32_t limit_base; | 42 | | #else | 43 | | int32_t early_exit; | 44 | | #endif | 45 | 36.8M | uint32_t chain_length = s->max_chain_length; | 46 | 36.8M | uint32_t nice_match = (uint32_t)s->nice_match; | 47 | 36.8M | uint32_t best_len, offset; | 48 | 36.8M | uint32_t lookahead = s->lookahead; | 49 | 36.8M | uint32_t match_offset = 0; | 50 | 36.8M | uint64_t scan_start; | 51 | 36.8M | uint64_t scan_end; | 52 | | | 53 | | /* The code is optimized for STD_MAX_MATCH-2 multiple of 16. */ | 54 | 36.8M | Assert(STD_MAX_MATCH == 258, "Code too clever"); | 55 | | | 56 | 36.8M | best_len = s->prev_length ? s->prev_length : STD_MIN_MATCH-1; | 57 | 36.8M | if (UNLIKELY(best_len >= lookahead)) | 58 | 4.17k | 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 | 36.8M | offset = best_len >= sizeof(uint64_t) ? best_len - 7 : 0; | 65 | | | 66 | 36.8M | scan = window + strstart; | 67 | 36.8M | scan_start = zng_memread_8(scan); | 68 | 36.8M | scan_end = zng_memread_8(scan+offset); | 69 | 36.8M | mbase_end = (mbase_start+offset); | 70 | | | 71 | | /* Do not waste too much time if we already have a good match */ | 72 | 36.8M | if (UNLIKELY(best_len >= s->good_match)) | 73 | 94.2k | 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 | 36.8M | limit = strstart > MAX_DIST(s) ? (strstart - MAX_DIST(s)) : 0; | 79 | 36.8M | #ifdef LONGEST_MATCH_ROLL | 80 | 36.8M | limit_base = limit; | 81 | 36.8M | if (best_len >= STD_MIN_MATCH) { | 82 | | /* We're continuing search (lazy evaluation). */ | 83 | 6.60M | uint32_t hash; | 84 | 6.60M | 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 | 6.60M | hash = update_hash_roll(0, scan[1]); | 92 | 6.60M | hash = update_hash_roll(hash, scan[2]); | 93 | | | 94 | 28.8M | for (uint32_t i = 3; i <= best_len; i++) { | 95 | | // use update_hash_roll for deflate_slow | 96 | 22.1M | hash = update_hash_roll(hash, scan[i]); | 97 | | /* If we're starting with best_len >= 3, we can use offset search. */ | 98 | 22.1M | pos = head[hash]; | 99 | 22.1M | if (UNLIKELY(pos < cur_match)) { | 100 | 5.41M | match_offset = i - 2; | 101 | 5.41M | cur_match = pos; | 102 | 5.41M | } | 103 | 22.1M | } | 104 | | | 105 | | /* Update offset-dependent variables */ | 106 | 6.60M | limit = limit_base+match_offset; | 107 | 6.60M | if (UNLIKELY(cur_match <= limit)) | 108 | 1.57M | return best_len; | 109 | 5.03M | mbase_start -= match_offset; | 110 | 5.03M | mbase_end -= match_offset; | 111 | 5.03M | } | 112 | | #else | 113 | | early_exit = s->level < EARLY_EXIT_TRIGGER_LEVEL; | 114 | | #endif | 115 | 35.2M | Assert((unsigned long)strstart <= s->window_size - MIN_LOOKAHEAD, "need lookahead"); | 116 | 47.7M | for (;;) { | 117 | 47.7M | 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 | 47.7M | uint32_t len; | 128 | 47.7M | if (best_len < sizeof(uint64_t)) { | 129 | 43.0M | uint64_t cand_start = zng_memread_8(mbase_start + cur_match); | 130 | 43.0M | 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 | 41.0M | uint64_t first_mask = zng_first_bytes_mask64(best_len + 1); | 135 | 41.0M | 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 | 41.0M | if (UNLIKELY((diff & first_mask) == 0)) { | 139 | 7.18M | len = zng_first_diff_byte64(diff); | 140 | 7.18M | goto short_match_accept; | 141 | 7.18M | } | 142 | 33.8M | if (--chain_length == 0 || (cur_match = prev[cur_match & wmask]) <= limit) | 143 | 13.2M | return best_len; | 144 | 20.6M | cand_start = zng_memread_8(mbase_start + cur_match); | 145 | 20.6M | if (scan_start != cand_start) { | 146 | | /* Walk the remaining candidates with the chain advance kept inline. */ | 147 | 144M | for (;;) { | 148 | 144M | diff = scan_start ^ cand_start; | 149 | 144M | if (UNLIKELY((diff & first_mask) == 0)) { | 150 | 3.31M | len = zng_first_diff_byte64(diff); | 151 | 3.31M | goto short_match_accept; | 152 | 3.31M | } | 153 | 141M | if (--chain_length == 0 || (cur_match = prev[cur_match & wmask]) <= limit) | 154 | 17.2M | return best_len; | 155 | 124M | cand_start = zng_memread_8(mbase_start + cur_match); | 156 | 124M | if (scan_start == cand_start) | 157 | 62.0k | break; | 158 | 124M | } | 159 | 20.5M | } | 160 | 20.6M | } | 161 | | /* All 8 bytes match, fallthrough to compare256 for the tail. */ | 162 | 43.0M | } 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 | 56.1M | for (;;) { | 166 | | /* First check the end of the candidate at best_len+1 due to the higher | 167 | | * likelihood of a mismatch. */ | 168 | 56.1M | if (zng_memcmp_8(mbase_end+cur_match, &scan_end) == 0 && | 169 | 6.15M | zng_memcmp_8(mbase_start+cur_match, &scan_start) == 0) | 170 | 4.43M | break; | 171 | 51.7M | GOTO_NEXT_CHAIN; | 172 | 51.7M | } | 173 | 4.78M | } | 174 | 6.51M | len = COMPARE256(scan+2, mbase_start+cur_match+2) + 2; | 175 | 6.51M | Assert(scan+len <= window+(unsigned)(s->window_size-1), "wild scan"); | 176 | | | 177 | 6.51M | if (len > best_len) | 178 | 18.3M | short_match_accept: | 179 | 18.3M | { | 180 | 18.3M | uint32_t match_start = cur_match - match_offset; | 181 | 18.3M | 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 | 18.3M | if (UNLIKELY(len >= lookahead)) | 187 | 4.35k | return lookahead; | 188 | 14.4M | if (UNLIKELY(len >= nice_match)) | 189 | 1.74M | return len; | 190 | | | 191 | 12.6M | best_len = len; | 192 | | | 193 | 12.6M | offset = best_len >= sizeof(uint64_t) ? best_len - 7 : 0; | 194 | | | 195 | 12.6M | scan_end = zng_memread_8(scan+offset); | 196 | | | 197 | 12.6M | #ifdef LONGEST_MATCH_ROLL | 198 | | /* Look for a better string offset */ | 199 | 12.6M | if (UNLIKELY(len > STD_MIN_MATCH && match_start + len < strstart)) { | 200 | 6.22M | const unsigned char *scan_endstr; | 201 | 6.22M | uint32_t hash; | 202 | 6.22M | uint32_t pos, next_pos; | 203 | | | 204 | | /* Go back to offset 0 */ | 205 | 6.22M | cur_match -= match_offset; | 206 | 6.22M | match_offset = 0; | 207 | 6.22M | next_pos = cur_match; | 208 | 158M | for (uint32_t i = 0; i <= len - STD_MIN_MATCH; i++) { | 209 | 153M | pos = prev[(cur_match + i) & wmask]; | 210 | 153M | if (UNLIKELY(pos < next_pos)) { | 211 | | /* Hash chain is more distant, use it */ | 212 | 8.05M | if (UNLIKELY(pos <= limit_base + i)) | 213 | 1.42M | return best_len; | 214 | 6.62M | next_pos = pos; | 215 | 6.62M | match_offset = i; | 216 | 6.62M | } | 217 | 153M | } | 218 | | /* Switch cur_match to next_pos chain */ | 219 | 4.79M | 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 | 4.79M | scan_endstr = scan + len - (STD_MIN_MATCH-1); | 227 | | | 228 | 4.79M | hash = update_hash_roll(0, scan_endstr[0]); | 229 | 4.79M | hash = update_hash_roll(hash, scan_endstr[1]); | 230 | 4.79M | hash = update_hash_roll(hash, scan_endstr[2]); | 231 | | | 232 | 4.79M | pos = head[hash]; | 233 | 4.79M | if (UNLIKELY(pos < cur_match)) { | 234 | 855k | match_offset = len - (STD_MIN_MATCH-1); | 235 | 855k | if (pos <= limit_base + match_offset) | 236 | 408k | return best_len; | 237 | 446k | cur_match = pos; | 238 | 446k | } | 239 | | | 240 | | /* Update offset-dependent variables */ | 241 | 4.38M | limit = limit_base+match_offset; | 242 | 4.38M | mbase_start = window-match_offset; | 243 | 4.38M | mbase_end = (mbase_start+offset); | 244 | 4.38M | continue; | 245 | 4.79M | } | 246 | 6.45M | #endif | 247 | 6.45M | mbase_end = (mbase_start+offset); | 248 | 6.45M | } | 249 | | #ifndef LONGEST_MATCH_ROLL | 250 | | 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 | | break; | 255 | | } | 256 | | #endif | 257 | 9.04M | GOTO_NEXT_CHAIN; | 258 | 9.04M | } | 259 | 0 | return best_len; | 260 | 35.2M | } |
Unexecuted instantiation: longest_match_avx512 Unexecuted instantiation: longest_match_roll_avx512 |
261 | | |
262 | | #undef LONGEST_MATCH_ROLL |
263 | | #undef LONGEST_MATCH |