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 | 53.1k | if (--chain_length && (cur_match = prev[cur_match & wmask]) > limit) \ |
17 | 53.1k | 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 | | * The LONGEST_MATCH_SLOW variant spends more time to attempt to find longer |
29 | | * matches once a match has already been found. |
30 | | */ |
31 | 1.74M | Z_INTERNAL uint32_t LONGEST_MATCH(deflate_state *const s, uint32_t cur_match) { |
32 | 1.74M | const unsigned wmask = W_MASK(s); |
33 | 1.74M | unsigned int strstart = s->strstart; |
34 | 1.74M | const unsigned char *window = s->window; |
35 | 1.74M | const Pos *prev = s->prev; |
36 | | #ifdef LONGEST_MATCH_SLOW |
37 | | const Pos *head = s->head; |
38 | | #endif |
39 | 1.74M | const unsigned char *scan; |
40 | 1.74M | const unsigned char *mbase_start = window; |
41 | 1.74M | const unsigned char *mbase_end; |
42 | 1.74M | uint32_t limit; |
43 | | #ifdef LONGEST_MATCH_SLOW |
44 | | uint32_t limit_base; |
45 | | #else |
46 | | int32_t early_exit; |
47 | | #endif |
48 | 1.74M | uint32_t chain_length = s->max_chain_length; |
49 | 1.74M | uint32_t nice_match = (uint32_t)s->nice_match; |
50 | 1.74M | uint32_t best_len, offset; |
51 | 1.74M | uint32_t lookahead = s->lookahead; |
52 | 1.74M | uint32_t match_offset = 0; |
53 | 1.74M | uint64_t scan_start; |
54 | 1.74M | uint64_t scan_end; |
55 | | |
56 | | /* The code is optimized for STD_MAX_MATCH-2 multiple of 16. */ |
57 | 1.74M | Assert(STD_MAX_MATCH == 258, "Code too clever"); |
58 | | |
59 | 1.74M | scan = window + strstart; |
60 | 1.74M | best_len = s->prev_length ? s->prev_length : STD_MIN_MATCH-1; |
61 | | |
62 | | /* Calculate read offset which should only extend an extra byte |
63 | | * to find the next best match length. |
64 | | */ |
65 | 1.74M | offset = best_len-1; |
66 | 1.74M | if (best_len >= sizeof(uint32_t)) { |
67 | 2.76k | offset -= 2; |
68 | 2.76k | if (best_len >= sizeof(uint64_t)) |
69 | 2.57k | offset -= 4; |
70 | 2.76k | } |
71 | | |
72 | 1.74M | scan_start = zng_memread_8(scan); |
73 | 1.74M | scan_end = zng_memread_8(scan+offset); |
74 | 1.74M | mbase_end = (mbase_start+offset); |
75 | | |
76 | | /* Do not waste too much time if we already have a good match */ |
77 | 1.74M | if (best_len >= s->good_match) |
78 | 2.20k | chain_length >>= 2; |
79 | | |
80 | | /* Stop when cur_match becomes <= limit. To simplify the code, |
81 | | * we prevent matches with the string of window index 0 |
82 | | */ |
83 | 1.74M | limit = strstart > MAX_DIST(s) ? (strstart - MAX_DIST(s)) : 0; |
84 | | #ifdef LONGEST_MATCH_SLOW |
85 | | limit_base = limit; |
86 | 1.74M | if (best_len >= STD_MIN_MATCH) { |
87 | | /* We're continuing search (lazy evaluation). */ |
88 | 2.76k | uint32_t hash; |
89 | 2.76k | uint32_t pos; |
90 | | |
91 | | /* Find a most distant chain starting from scan with index=1 (index=0 corresponds |
92 | | * to cur_match). We cannot use s->prev[strstart+1,...] immediately, because |
93 | | * these strings are not yet inserted into the hash table. |
94 | | */ |
95 | | // use update_hash_roll for deflate_slow |
96 | | hash = update_hash_roll(0, scan[1]); |
97 | | hash = update_hash_roll(hash, scan[2]); |
98 | | |
99 | 321k | for (uint32_t i = 3; i <= best_len; i++) { |
100 | | // use update_hash_roll for deflate_slow |
101 | 318k | hash = update_hash_roll(hash, scan[i]); |
102 | | /* If we're starting with best_len >= 3, we can use offset search. */ |
103 | 318k | pos = head[hash]; |
104 | 318k | if (pos < cur_match) { |
105 | 0 | match_offset = i - 2; |
106 | 0 | cur_match = pos; |
107 | 0 | } |
108 | 318k | } |
109 | | |
110 | | /* Update offset-dependent variables */ |
111 | 2.76k | limit = limit_base+match_offset; |
112 | 2.76k | if (cur_match <= limit) |
113 | 0 | goto break_matching; |
114 | 2.76k | mbase_start -= match_offset; |
115 | 2.76k | mbase_end -= match_offset; |
116 | 2.76k | } |
117 | | #else |
118 | 0 | early_exit = s->level < EARLY_EXIT_TRIGGER_LEVEL; |
119 | | #endif |
120 | 1.74M | Assert((unsigned long)strstart <= s->window_size - MIN_LOOKAHEAD, "need lookahead"); |
121 | 2.11M | for (;;) { |
122 | 2.11M | if (cur_match >= strstart) |
123 | 0 | break; |
124 | | |
125 | | /* Skip to next match if the match length cannot increase or if the match length is |
126 | | * less than 2. Note that the checks below for insufficient lookahead only occur |
127 | | * occasionally for performance reasons. |
128 | | * Therefore uninitialized memory will be accessed and conditional jumps will be made |
129 | | * that depend on those values. However the length of the match is limited to the |
130 | | * lookahead, so the output of deflate is not affected by the uninitialized values. |
131 | | */ |
132 | 2.11M | if (best_len < sizeof(uint32_t)) { |
133 | 1.74M | for (;;) { |
134 | 1.74M | if (zng_memcmp_2(mbase_end+cur_match, &scan_end) == 0 && |
135 | 1.74M | zng_memcmp_2(mbase_start+cur_match, &scan_start) == 0) |
136 | 1.74M | break; |
137 | 429 | GOTO_NEXT_CHAIN; |
138 | 429 | } |
139 | 1.74M | } else if (best_len >= sizeof(uint64_t)) { |
140 | 388k | for (;;) { |
141 | 388k | if (zng_memcmp_8(mbase_end+cur_match, &scan_end) == 0 && |
142 | 371k | zng_memcmp_8(mbase_start+cur_match, &scan_start) == 0) |
143 | 368k | break; |
144 | 19.9k | GOTO_NEXT_CHAIN; |
145 | 19.9k | } |
146 | 368k | } else { |
147 | 6.15k | for (;;) { |
148 | 6.15k | if (zng_memcmp_4(mbase_end+cur_match, &scan_end) == 0 && |
149 | 5.59k | zng_memcmp_4(mbase_start+cur_match, &scan_start) == 0) |
150 | 5.54k | break; |
151 | 615 | GOTO_NEXT_CHAIN; |
152 | 615 | } |
153 | 5.54k | } |
154 | 2.11M | uint32_t len = COMPARE256(scan+2, mbase_start+cur_match+2) + 2; |
155 | 2.11M | Assert(scan+len <= window+(unsigned)(s->window_size-1), "wild scan"); |
156 | | |
157 | 2.11M | if (len > best_len) { |
158 | 2.08M | uint32_t match_start = cur_match - match_offset; |
159 | 2.08M | s->match_start = match_start; |
160 | | |
161 | | /* Do not look for better matches if the current match reaches |
162 | | * or exceeds the end of the input. |
163 | | */ |
164 | 2.08M | if (len >= lookahead) |
165 | 5.65k | return lookahead; |
166 | 2.08M | if (len >= nice_match) |
167 | 1.73M | return len; |
168 | | |
169 | 341k | best_len = len; |
170 | | |
171 | 341k | offset = best_len-1; |
172 | 341k | if (best_len >= sizeof(uint32_t)) { |
173 | 340k | offset -= 2; |
174 | 340k | if (best_len >= sizeof(uint64_t)) |
175 | 334k | offset -= 4; |
176 | 340k | } |
177 | | |
178 | 341k | scan_end = zng_memread_8(scan+offset); |
179 | | |
180 | | #ifdef LONGEST_MATCH_SLOW |
181 | | /* Look for a better string offset */ |
182 | 341k | if (UNLIKELY(len > STD_MIN_MATCH && match_start + len < strstart)) { |
183 | 340k | const unsigned char *scan_endstr; |
184 | 340k | uint32_t hash; |
185 | 340k | uint32_t pos, next_pos; |
186 | | |
187 | | /* Go back to offset 0 */ |
188 | | cur_match -= match_offset; |
189 | | match_offset = 0; |
190 | | next_pos = cur_match; |
191 | 44.0M | for (uint32_t i = 0; i <= len - STD_MIN_MATCH; i++) { |
192 | 43.7M | pos = prev[(cur_match + i) & wmask]; |
193 | 43.7M | if (pos < next_pos) { |
194 | | /* Hash chain is more distant, use it */ |
195 | 340k | if (pos <= limit_base + i) |
196 | 0 | goto break_matching; |
197 | 340k | next_pos = pos; |
198 | 340k | match_offset = i; |
199 | 340k | } |
200 | 43.7M | } |
201 | | /* Switch cur_match to next_pos chain */ |
202 | 340k | cur_match = next_pos; |
203 | | |
204 | | /* Try hash head at len-(STD_MIN_MATCH-1) position to see if we could get |
205 | | * a better cur_match at the end of string. Using (STD_MIN_MATCH-1) lets |
206 | | * us include one more byte into hash - the byte which will be checked |
207 | | * in main loop now, and which allows to grow match by 1. |
208 | | */ |
209 | 340k | scan_endstr = scan + len - (STD_MIN_MATCH+1); |
210 | | |
211 | | // use update_hash_roll for deflate_slow |
212 | 340k | hash = update_hash_roll(0, scan_endstr[0]); |
213 | 340k | hash = update_hash_roll(hash, scan_endstr[1]); |
214 | 340k | hash = update_hash_roll(hash, scan_endstr[2]); |
215 | | |
216 | 340k | pos = head[hash]; |
217 | 340k | if (pos < cur_match) { |
218 | 0 | match_offset = len - (STD_MIN_MATCH+1); |
219 | 0 | if (pos <= limit_base + match_offset) |
220 | 0 | goto break_matching; |
221 | 0 | cur_match = pos; |
222 | 0 | } |
223 | | |
224 | | /* Update offset-dependent variables */ |
225 | 340k | limit = limit_base+match_offset; |
226 | 340k | mbase_start = window-match_offset; |
227 | 340k | mbase_end = (mbase_start+offset); |
228 | 340k | continue; |
229 | 340k | } |
230 | 1.33k | #endif |
231 | 1.33k | mbase_end = (mbase_start+offset); |
232 | 1.33k | } |
233 | | #ifndef LONGEST_MATCH_SLOW |
234 | 0 | else if (UNLIKELY(early_exit)) { |
235 | | /* The probability of finding a match later if we here is pretty low, so for |
236 | | * performance it's best to outright stop here for the lower compression levels |
237 | | */ |
238 | 0 | break; |
239 | 0 | } |
240 | 0 | #endif |
241 | 32.1k | GOTO_NEXT_CHAIN; |
242 | 32.1k | } |
243 | 0 | return best_len; |
244 | | |
245 | | #ifdef LONGEST_MATCH_SLOW |
246 | 0 | break_matching: |
247 | | |
248 | 0 | if (best_len < lookahead) |
249 | 0 | return best_len; |
250 | | |
251 | 0 | return lookahead; |
252 | | #endif |
253 | 0 | } Unexecuted instantiation: longest_match_sse2 Unexecuted instantiation: longest_match_slow_sse2 Unexecuted instantiation: longest_match_avx2 Line | Count | Source | 31 | 1.74M | Z_INTERNAL uint32_t LONGEST_MATCH(deflate_state *const s, uint32_t cur_match) { | 32 | 1.74M | const unsigned wmask = W_MASK(s); | 33 | 1.74M | unsigned int strstart = s->strstart; | 34 | 1.74M | const unsigned char *window = s->window; | 35 | 1.74M | const Pos *prev = s->prev; | 36 | 1.74M | #ifdef LONGEST_MATCH_SLOW | 37 | 1.74M | const Pos *head = s->head; | 38 | 1.74M | #endif | 39 | 1.74M | const unsigned char *scan; | 40 | 1.74M | const unsigned char *mbase_start = window; | 41 | 1.74M | const unsigned char *mbase_end; | 42 | 1.74M | uint32_t limit; | 43 | 1.74M | #ifdef LONGEST_MATCH_SLOW | 44 | 1.74M | uint32_t limit_base; | 45 | | #else | 46 | | int32_t early_exit; | 47 | | #endif | 48 | 1.74M | uint32_t chain_length = s->max_chain_length; | 49 | 1.74M | uint32_t nice_match = (uint32_t)s->nice_match; | 50 | 1.74M | uint32_t best_len, offset; | 51 | 1.74M | uint32_t lookahead = s->lookahead; | 52 | 1.74M | uint32_t match_offset = 0; | 53 | 1.74M | uint64_t scan_start; | 54 | 1.74M | uint64_t scan_end; | 55 | | | 56 | | /* The code is optimized for STD_MAX_MATCH-2 multiple of 16. */ | 57 | 1.74M | Assert(STD_MAX_MATCH == 258, "Code too clever"); | 58 | | | 59 | 1.74M | scan = window + strstart; | 60 | 1.74M | best_len = s->prev_length ? s->prev_length : STD_MIN_MATCH-1; | 61 | | | 62 | | /* Calculate read offset which should only extend an extra byte | 63 | | * to find the next best match length. | 64 | | */ | 65 | 1.74M | offset = best_len-1; | 66 | 1.74M | if (best_len >= sizeof(uint32_t)) { | 67 | 2.76k | offset -= 2; | 68 | 2.76k | if (best_len >= sizeof(uint64_t)) | 69 | 2.57k | offset -= 4; | 70 | 2.76k | } | 71 | | | 72 | 1.74M | scan_start = zng_memread_8(scan); | 73 | 1.74M | scan_end = zng_memread_8(scan+offset); | 74 | 1.74M | mbase_end = (mbase_start+offset); | 75 | | | 76 | | /* Do not waste too much time if we already have a good match */ | 77 | 1.74M | if (best_len >= s->good_match) | 78 | 2.20k | chain_length >>= 2; | 79 | | | 80 | | /* Stop when cur_match becomes <= limit. To simplify the code, | 81 | | * we prevent matches with the string of window index 0 | 82 | | */ | 83 | 1.74M | limit = strstart > MAX_DIST(s) ? (strstart - MAX_DIST(s)) : 0; | 84 | 1.74M | #ifdef LONGEST_MATCH_SLOW | 85 | 1.74M | limit_base = limit; | 86 | 1.74M | if (best_len >= STD_MIN_MATCH) { | 87 | | /* We're continuing search (lazy evaluation). */ | 88 | 2.76k | uint32_t hash; | 89 | 2.76k | uint32_t pos; | 90 | | | 91 | | /* Find a most distant chain starting from scan with index=1 (index=0 corresponds | 92 | | * to cur_match). We cannot use s->prev[strstart+1,...] immediately, because | 93 | | * these strings are not yet inserted into the hash table. | 94 | | */ | 95 | | // use update_hash_roll for deflate_slow | 96 | 2.76k | hash = update_hash_roll(0, scan[1]); | 97 | 2.76k | hash = update_hash_roll(hash, scan[2]); | 98 | | | 99 | 321k | for (uint32_t i = 3; i <= best_len; i++) { | 100 | | // use update_hash_roll for deflate_slow | 101 | 318k | hash = update_hash_roll(hash, scan[i]); | 102 | | /* If we're starting with best_len >= 3, we can use offset search. */ | 103 | 318k | pos = head[hash]; | 104 | 318k | if (pos < cur_match) { | 105 | 0 | match_offset = i - 2; | 106 | 0 | cur_match = pos; | 107 | 0 | } | 108 | 318k | } | 109 | | | 110 | | /* Update offset-dependent variables */ | 111 | 2.76k | limit = limit_base+match_offset; | 112 | 2.76k | if (cur_match <= limit) | 113 | 0 | goto break_matching; | 114 | 2.76k | mbase_start -= match_offset; | 115 | 2.76k | mbase_end -= match_offset; | 116 | 2.76k | } | 117 | | #else | 118 | | early_exit = s->level < EARLY_EXIT_TRIGGER_LEVEL; | 119 | | #endif | 120 | 1.74M | Assert((unsigned long)strstart <= s->window_size - MIN_LOOKAHEAD, "need lookahead"); | 121 | 2.11M | for (;;) { | 122 | 2.11M | if (cur_match >= strstart) | 123 | 0 | break; | 124 | | | 125 | | /* Skip to next match if the match length cannot increase or if the match length is | 126 | | * less than 2. Note that the checks below for insufficient lookahead only occur | 127 | | * occasionally for performance reasons. | 128 | | * Therefore uninitialized memory will be accessed and conditional jumps will be made | 129 | | * that depend on those values. However the length of the match is limited to the | 130 | | * lookahead, so the output of deflate is not affected by the uninitialized values. | 131 | | */ | 132 | 2.11M | if (best_len < sizeof(uint32_t)) { | 133 | 1.74M | for (;;) { | 134 | 1.74M | if (zng_memcmp_2(mbase_end+cur_match, &scan_end) == 0 && | 135 | 1.74M | zng_memcmp_2(mbase_start+cur_match, &scan_start) == 0) | 136 | 1.74M | break; | 137 | 429 | GOTO_NEXT_CHAIN; | 138 | 429 | } | 139 | 1.74M | } else if (best_len >= sizeof(uint64_t)) { | 140 | 388k | for (;;) { | 141 | 388k | if (zng_memcmp_8(mbase_end+cur_match, &scan_end) == 0 && | 142 | 371k | zng_memcmp_8(mbase_start+cur_match, &scan_start) == 0) | 143 | 368k | break; | 144 | 19.9k | GOTO_NEXT_CHAIN; | 145 | 19.9k | } | 146 | 368k | } else { | 147 | 6.15k | for (;;) { | 148 | 6.15k | if (zng_memcmp_4(mbase_end+cur_match, &scan_end) == 0 && | 149 | 5.59k | zng_memcmp_4(mbase_start+cur_match, &scan_start) == 0) | 150 | 5.54k | break; | 151 | 615 | GOTO_NEXT_CHAIN; | 152 | 615 | } | 153 | 5.54k | } | 154 | 2.11M | uint32_t len = COMPARE256(scan+2, mbase_start+cur_match+2) + 2; | 155 | 2.11M | Assert(scan+len <= window+(unsigned)(s->window_size-1), "wild scan"); | 156 | | | 157 | 2.11M | if (len > best_len) { | 158 | 2.08M | uint32_t match_start = cur_match - match_offset; | 159 | 2.08M | s->match_start = match_start; | 160 | | | 161 | | /* Do not look for better matches if the current match reaches | 162 | | * or exceeds the end of the input. | 163 | | */ | 164 | 2.08M | if (len >= lookahead) | 165 | 5.65k | return lookahead; | 166 | 2.08M | if (len >= nice_match) | 167 | 1.73M | return len; | 168 | | | 169 | 341k | best_len = len; | 170 | | | 171 | 341k | offset = best_len-1; | 172 | 341k | if (best_len >= sizeof(uint32_t)) { | 173 | 340k | offset -= 2; | 174 | 340k | if (best_len >= sizeof(uint64_t)) | 175 | 334k | offset -= 4; | 176 | 340k | } | 177 | | | 178 | 341k | scan_end = zng_memread_8(scan+offset); | 179 | | | 180 | 341k | #ifdef LONGEST_MATCH_SLOW | 181 | | /* Look for a better string offset */ | 182 | 341k | if (UNLIKELY(len > STD_MIN_MATCH && match_start + len < strstart)) { | 183 | 340k | const unsigned char *scan_endstr; | 184 | 340k | uint32_t hash; | 185 | 340k | uint32_t pos, next_pos; | 186 | | | 187 | | /* Go back to offset 0 */ | 188 | 340k | cur_match -= match_offset; | 189 | 340k | match_offset = 0; | 190 | 340k | next_pos = cur_match; | 191 | 44.0M | for (uint32_t i = 0; i <= len - STD_MIN_MATCH; i++) { | 192 | 43.7M | pos = prev[(cur_match + i) & wmask]; | 193 | 43.7M | if (pos < next_pos) { | 194 | | /* Hash chain is more distant, use it */ | 195 | 340k | if (pos <= limit_base + i) | 196 | 0 | goto break_matching; | 197 | 340k | next_pos = pos; | 198 | 340k | match_offset = i; | 199 | 340k | } | 200 | 43.7M | } | 201 | | /* Switch cur_match to next_pos chain */ | 202 | 340k | cur_match = next_pos; | 203 | | | 204 | | /* Try hash head at len-(STD_MIN_MATCH-1) position to see if we could get | 205 | | * a better cur_match at the end of string. Using (STD_MIN_MATCH-1) lets | 206 | | * us include one more byte into hash - the byte which will be checked | 207 | | * in main loop now, and which allows to grow match by 1. | 208 | | */ | 209 | 340k | scan_endstr = scan + len - (STD_MIN_MATCH+1); | 210 | | | 211 | | // use update_hash_roll for deflate_slow | 212 | 340k | hash = update_hash_roll(0, scan_endstr[0]); | 213 | 340k | hash = update_hash_roll(hash, scan_endstr[1]); | 214 | 340k | hash = update_hash_roll(hash, scan_endstr[2]); | 215 | | | 216 | 340k | pos = head[hash]; | 217 | 340k | if (pos < cur_match) { | 218 | 0 | match_offset = len - (STD_MIN_MATCH+1); | 219 | 0 | if (pos <= limit_base + match_offset) | 220 | 0 | goto break_matching; | 221 | 0 | cur_match = pos; | 222 | 0 | } | 223 | | | 224 | | /* Update offset-dependent variables */ | 225 | 340k | limit = limit_base+match_offset; | 226 | 340k | mbase_start = window-match_offset; | 227 | 340k | mbase_end = (mbase_start+offset); | 228 | 340k | continue; | 229 | 340k | } | 230 | 1.33k | #endif | 231 | 1.33k | mbase_end = (mbase_start+offset); | 232 | 1.33k | } | 233 | | #ifndef LONGEST_MATCH_SLOW | 234 | | else if (UNLIKELY(early_exit)) { | 235 | | /* The probability of finding a match later if we here is pretty low, so for | 236 | | * performance it's best to outright stop here for the lower compression levels | 237 | | */ | 238 | | break; | 239 | | } | 240 | | #endif | 241 | 32.1k | GOTO_NEXT_CHAIN; | 242 | 32.1k | } | 243 | 0 | return best_len; | 244 | | | 245 | 0 | #ifdef LONGEST_MATCH_SLOW | 246 | 0 | break_matching: | 247 | |
| 248 | 0 | if (best_len < lookahead) | 249 | 0 | return best_len; | 250 | | | 251 | 0 | return lookahead; | 252 | 0 | #endif | 253 | 0 | } |
Unexecuted instantiation: longest_match_avx512 Unexecuted instantiation: longest_match_slow_avx512 |
254 | | |
255 | | #undef LONGEST_MATCH_SLOW |
256 | | #undef LONGEST_MATCH |