Coverage Report

Created: 2026-02-26 06:53

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/zlib-ng/match_tpl.h
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
125M
#define EARLY_EXIT_TRIGGER_LEVEL 5
14
15
#define GOTO_NEXT_CHAIN \
16
318M
    if (--chain_length && (cur_match = prev[cur_match & wmask]) > limit) \
17
194M
        continue; \
18
124M
    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
125M
Z_INTERNAL uint32_t LONGEST_MATCH(deflate_state *const s, uint32_t cur_match) {
32
125M
    const unsigned wmask = W_MASK(s);
33
125M
    unsigned int strstart = s->strstart;
34
125M
    const unsigned char *window = s->window;
35
125M
    const Pos *prev = s->prev;
36
#ifdef LONGEST_MATCH_SLOW
37
    const Pos *head = s->head;
38
#endif
39
125M
    const unsigned char *scan;
40
125M
    const unsigned char *mbase_start = window;
41
125M
    const unsigned char *mbase_end;
42
125M
    uint32_t limit;
43
#ifdef LONGEST_MATCH_SLOW
44
    uint32_t limit_base;
45
#else
46
    int32_t early_exit;
47
#endif
48
125M
    uint32_t chain_length = s->max_chain_length;
49
125M
    uint32_t nice_match = (uint32_t)s->nice_match;
50
125M
    uint32_t best_len, offset;
51
125M
    uint32_t lookahead = s->lookahead;
52
125M
    uint32_t match_offset = 0;
53
125M
    uint64_t scan_start;
54
125M
    uint64_t scan_end;
55
56
    /* The code is optimized for STD_MAX_MATCH-2 multiple of 16. */
57
125M
    Assert(STD_MAX_MATCH == 258, "Code too clever");
58
59
125M
    scan = window + strstart;
60
125M
    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
125M
    offset = best_len-1;
66
125M
    if (best_len >= sizeof(uint32_t)) {
67
1.89M
        offset -= 2;
68
1.89M
        if (best_len >= sizeof(uint64_t))
69
568k
            offset -= 4;
70
1.89M
    }
71
72
125M
    scan_start = zng_memread_8(scan);
73
125M
    scan_end = zng_memread_8(scan+offset);
74
125M
    mbase_end  = (mbase_start+offset);
75
76
    /* Do not waste too much time if we already have a good match */
77
125M
    if (best_len >= s->good_match)
78
568k
        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
125M
    limit = strstart > MAX_DIST(s) ? (strstart - MAX_DIST(s)) : 0;
84
#ifdef LONGEST_MATCH_SLOW
85
    limit_base = limit;
86
0
    if (best_len >= STD_MIN_MATCH) {
87
        /* We're continuing search (lazy evaluation). */
88
0
        uint32_t hash;
89
0
        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
0
        for (uint32_t i = 3; i <= best_len; i++) {
100
            // use update_hash_roll for deflate_slow
101
0
            hash = update_hash_roll(hash, scan[i]);
102
            /* If we're starting with best_len >= 3, we can use offset search. */
103
0
            pos = head[hash];
104
0
            if (pos < cur_match) {
105
0
                match_offset = i - 2;
106
0
                cur_match = pos;
107
0
            }
108
0
        }
109
110
        /* Update offset-dependent variables */
111
0
        limit = limit_base+match_offset;
112
0
        if (cur_match <= limit)
113
0
            goto break_matching;
114
0
        mbase_start -= match_offset;
115
0
        mbase_end -= match_offset;
116
0
    }
117
#else
118
125M
    early_exit = s->level < EARLY_EXIT_TRIGGER_LEVEL;
119
#endif
120
0
    Assert((unsigned long)strstart <= s->window_size - MIN_LOOKAHEAD, "need lookahead");
121
141M
    for (;;) {
122
141M
        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
141M
        if (best_len < sizeof(uint32_t)) {
133
157M
            for (;;) {
134
157M
                if (zng_memcmp_2(mbase_end+cur_match, &scan_end) == 0 &&
135
11.4M
                    zng_memcmp_2(mbase_start+cur_match, &scan_start) == 0)
136
11.4M
                    break;
137
146M
                GOTO_NEXT_CHAIN;
138
146M
            }
139
123M
        } else if (best_len >= sizeof(uint64_t)) {
140
64.7M
            for (;;) {
141
64.7M
                if (zng_memcmp_8(mbase_end+cur_match, &scan_end) == 0 &&
142
4.96M
                    zng_memcmp_8(mbase_start+cur_match, &scan_start) == 0)
143
4.51M
                    break;
144
60.2M
                GOTO_NEXT_CHAIN;
145
60.2M
            }
146
11.1M
        } else {
147
97.7M
            for (;;) {
148
97.7M
                if (zng_memcmp_4(mbase_end+cur_match, &scan_end) == 0 &&
149
4.07M
                    zng_memcmp_4(mbase_start+cur_match, &scan_start) == 0)
150
4.07M
                    break;
151
93.6M
                GOTO_NEXT_CHAIN;
152
93.6M
            }
153
11.1M
        }
154
20.0M
        uint32_t len = COMPARE256(scan+2, mbase_start+cur_match+2) + 2;
155
20.0M
        Assert(scan+len <= window+(unsigned)(s->window_size-1), "wild scan");
156
157
20.0M
        if (len > best_len) {
158
19.3M
            uint32_t match_start = cur_match - match_offset;
159
19.3M
            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
19.3M
            if (len >= lookahead)
165
4.85k
                return lookahead;
166
19.3M
            if (len >= nice_match)
167
1.23M
                return len;
168
169
18.0M
            best_len = len;
170
171
18.0M
            offset = best_len-1;
172
18.0M
            if (best_len >= sizeof(uint32_t)) {
173
18.0M
                offset -= 2;
174
18.0M
                if (best_len >= sizeof(uint64_t))
175
6.18M
                    offset -= 4;
176
18.0M
            }
177
178
18.0M
            scan_end = zng_memread_8(scan+offset);
179
180
#ifdef LONGEST_MATCH_SLOW
181
            /* Look for a better string offset */
182
0
            if (UNLIKELY(len > STD_MIN_MATCH && match_start + len < strstart)) {
183
0
                const unsigned char *scan_endstr;
184
0
                uint32_t hash;
185
0
                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
0
                for (uint32_t i = 0; i <= len - STD_MIN_MATCH; i++) {
192
0
                    pos = prev[(cur_match + i) & wmask];
193
0
                    if (pos < next_pos) {
194
                        /* Hash chain is more distant, use it */
195
0
                        if (pos <= limit_base + i)
196
0
                            goto break_matching;
197
0
                        next_pos = pos;
198
0
                        match_offset = i;
199
0
                    }
200
0
                }
201
                /* Switch cur_match to next_pos chain */
202
0
                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
0
                scan_endstr = scan + len - (STD_MIN_MATCH+1);
210
211
                // use update_hash_roll for deflate_slow
212
0
                hash = update_hash_roll(0, scan_endstr[0]);
213
0
                hash = update_hash_roll(hash, scan_endstr[1]);
214
0
                hash = update_hash_roll(hash, scan_endstr[2]);
215
216
0
                pos = head[hash];
217
0
                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
0
                limit = limit_base+match_offset;
226
0
                mbase_start = window-match_offset;
227
0
                mbase_end = (mbase_start+offset);
228
0
                continue;
229
0
            }
230
0
#endif
231
0
            mbase_end = (mbase_start+offset);
232
0
        }
233
#ifndef LONGEST_MATCH_SLOW
234
729k
        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
18.8M
#endif
241
18.8M
        GOTO_NEXT_CHAIN;
242
18.8M
    }
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
longest_match_avx2
Line
Count
Source
31
125M
Z_INTERNAL uint32_t LONGEST_MATCH(deflate_state *const s, uint32_t cur_match) {
32
125M
    const unsigned wmask = W_MASK(s);
33
125M
    unsigned int strstart = s->strstart;
34
125M
    const unsigned char *window = s->window;
35
125M
    const Pos *prev = s->prev;
36
#ifdef LONGEST_MATCH_SLOW
37
    const Pos *head = s->head;
38
#endif
39
125M
    const unsigned char *scan;
40
125M
    const unsigned char *mbase_start = window;
41
125M
    const unsigned char *mbase_end;
42
125M
    uint32_t limit;
43
#ifdef LONGEST_MATCH_SLOW
44
    uint32_t limit_base;
45
#else
46
125M
    int32_t early_exit;
47
125M
#endif
48
125M
    uint32_t chain_length = s->max_chain_length;
49
125M
    uint32_t nice_match = (uint32_t)s->nice_match;
50
125M
    uint32_t best_len, offset;
51
125M
    uint32_t lookahead = s->lookahead;
52
125M
    uint32_t match_offset = 0;
53
125M
    uint64_t scan_start;
54
125M
    uint64_t scan_end;
55
56
    /* The code is optimized for STD_MAX_MATCH-2 multiple of 16. */
57
125M
    Assert(STD_MAX_MATCH == 258, "Code too clever");
58
59
125M
    scan = window + strstart;
60
125M
    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
125M
    offset = best_len-1;
66
125M
    if (best_len >= sizeof(uint32_t)) {
67
1.89M
        offset -= 2;
68
1.89M
        if (best_len >= sizeof(uint64_t))
69
568k
            offset -= 4;
70
1.89M
    }
71
72
125M
    scan_start = zng_memread_8(scan);
73
125M
    scan_end = zng_memread_8(scan+offset);
74
125M
    mbase_end  = (mbase_start+offset);
75
76
    /* Do not waste too much time if we already have a good match */
77
125M
    if (best_len >= s->good_match)
78
568k
        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
125M
    limit = strstart > MAX_DIST(s) ? (strstart - MAX_DIST(s)) : 0;
84
#ifdef LONGEST_MATCH_SLOW
85
    limit_base = limit;
86
    if (best_len >= STD_MIN_MATCH) {
87
        /* We're continuing search (lazy evaluation). */
88
        uint32_t hash;
89
        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
        for (uint32_t i = 3; i <= best_len; i++) {
100
            // use update_hash_roll for deflate_slow
101
            hash = update_hash_roll(hash, scan[i]);
102
            /* If we're starting with best_len >= 3, we can use offset search. */
103
            pos = head[hash];
104
            if (pos < cur_match) {
105
                match_offset = i - 2;
106
                cur_match = pos;
107
            }
108
        }
109
110
        /* Update offset-dependent variables */
111
        limit = limit_base+match_offset;
112
        if (cur_match <= limit)
113
            goto break_matching;
114
        mbase_start -= match_offset;
115
        mbase_end -= match_offset;
116
    }
117
#else
118
125M
    early_exit = s->level < EARLY_EXIT_TRIGGER_LEVEL;
119
125M
#endif
120
125M
    Assert((unsigned long)strstart <= s->window_size - MIN_LOOKAHEAD, "need lookahead");
121
141M
    for (;;) {
122
141M
        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
141M
        if (best_len < sizeof(uint32_t)) {
133
157M
            for (;;) {
134
157M
                if (zng_memcmp_2(mbase_end+cur_match, &scan_end) == 0 &&
135
11.4M
                    zng_memcmp_2(mbase_start+cur_match, &scan_start) == 0)
136
11.4M
                    break;
137
146M
                GOTO_NEXT_CHAIN;
138
146M
            }
139
123M
        } else if (best_len >= sizeof(uint64_t)) {
140
64.7M
            for (;;) {
141
64.7M
                if (zng_memcmp_8(mbase_end+cur_match, &scan_end) == 0 &&
142
4.96M
                    zng_memcmp_8(mbase_start+cur_match, &scan_start) == 0)
143
4.51M
                    break;
144
60.2M
                GOTO_NEXT_CHAIN;
145
60.2M
            }
146
11.1M
        } else {
147
97.7M
            for (;;) {
148
97.7M
                if (zng_memcmp_4(mbase_end+cur_match, &scan_end) == 0 &&
149
4.07M
                    zng_memcmp_4(mbase_start+cur_match, &scan_start) == 0)
150
4.07M
                    break;
151
93.6M
                GOTO_NEXT_CHAIN;
152
93.6M
            }
153
11.1M
        }
154
20.0M
        uint32_t len = COMPARE256(scan+2, mbase_start+cur_match+2) + 2;
155
20.0M
        Assert(scan+len <= window+(unsigned)(s->window_size-1), "wild scan");
156
157
20.0M
        if (len > best_len) {
158
19.3M
            uint32_t match_start = cur_match - match_offset;
159
19.3M
            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
19.3M
            if (len >= lookahead)
165
4.85k
                return lookahead;
166
19.3M
            if (len >= nice_match)
167
1.23M
                return len;
168
169
18.0M
            best_len = len;
170
171
18.0M
            offset = best_len-1;
172
18.0M
            if (best_len >= sizeof(uint32_t)) {
173
18.0M
                offset -= 2;
174
18.0M
                if (best_len >= sizeof(uint64_t))
175
6.18M
                    offset -= 4;
176
18.0M
            }
177
178
18.0M
            scan_end = zng_memread_8(scan+offset);
179
180
#ifdef LONGEST_MATCH_SLOW
181
            /* Look for a better string offset */
182
            if (UNLIKELY(len > STD_MIN_MATCH && match_start + len < strstart)) {
183
                const unsigned char *scan_endstr;
184
                uint32_t hash;
185
                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
                for (uint32_t i = 0; i <= len - STD_MIN_MATCH; i++) {
192
                    pos = prev[(cur_match + i) & wmask];
193
                    if (pos < next_pos) {
194
                        /* Hash chain is more distant, use it */
195
                        if (pos <= limit_base + i)
196
                            goto break_matching;
197
                        next_pos = pos;
198
                        match_offset = i;
199
                    }
200
                }
201
                /* Switch cur_match to next_pos chain */
202
                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
                scan_endstr = scan + len - (STD_MIN_MATCH+1);
210
211
                // use update_hash_roll for deflate_slow
212
                hash = update_hash_roll(0, scan_endstr[0]);
213
                hash = update_hash_roll(hash, scan_endstr[1]);
214
                hash = update_hash_roll(hash, scan_endstr[2]);
215
216
                pos = head[hash];
217
                if (pos < cur_match) {
218
                    match_offset = len - (STD_MIN_MATCH+1);
219
                    if (pos <= limit_base + match_offset)
220
                        goto break_matching;
221
                    cur_match = pos;
222
                }
223
224
                /* Update offset-dependent variables */
225
                limit = limit_base+match_offset;
226
                mbase_start = window-match_offset;
227
                mbase_end = (mbase_start+offset);
228
                continue;
229
            }
230
#endif
231
18.0M
            mbase_end = (mbase_start+offset);
232
18.0M
        }
233
729k
#ifndef LONGEST_MATCH_SLOW
234
729k
        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
18.8M
#endif
241
18.8M
        GOTO_NEXT_CHAIN;
242
18.8M
    }
243
0
    return best_len;
244
245
#ifdef LONGEST_MATCH_SLOW
246
break_matching:
247
248
    if (best_len < lookahead)
249
        return best_len;
250
251
    return lookahead;
252
#endif
253
125M
}
Unexecuted instantiation: longest_match_slow_avx2
Unexecuted instantiation: longest_match_avx512
Unexecuted instantiation: longest_match_slow_avx512
254
255
#undef LONGEST_MATCH_SLOW
256
#undef LONGEST_MATCH