Coverage Report

Created: 2026-08-13 06:29

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
127M
#define EARLY_EXIT_TRIGGER_LEVEL 5
14
15
#define GOTO_NEXT_CHAIN \
16
86.5M
    if (--chain_length && (cur_match = prev[cur_match & wmask]) > limit) \
17
80.6M
        continue; \
18
5.85M
    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
127M
Z_INTERNAL uint32_t LONGEST_MATCH(deflate_state *const s, uint32_t cur_match) {
29
127M
    const unsigned wmask = W_MASK(s);
30
127M
    unsigned int strstart = s->strstart;
31
127M
    const unsigned char *window = s->window;
32
127M
    const Pos *prev = s->prev;
33
#ifdef LONGEST_MATCH_ROLL
34
    const Pos *head = s->head;
35
#endif
36
127M
    const unsigned char *scan;
37
127M
    const unsigned char *mbase_start = window;
38
127M
    const unsigned char *mbase_end;
39
127M
    uint32_t limit;
40
#ifdef LONGEST_MATCH_ROLL
41
    uint32_t limit_base;
42
#else
43
    int32_t early_exit;
44
#endif
45
127M
    uint32_t chain_length = s->max_chain_length;
46
127M
    uint32_t nice_match = (uint32_t)s->nice_match;
47
127M
    uint32_t best_len, offset;
48
127M
    uint32_t lookahead = s->lookahead;
49
127M
    uint32_t match_offset = 0;
50
127M
    uint64_t scan_start;
51
127M
    uint64_t scan_end;
52
53
    /* The code is optimized for STD_MAX_MATCH-2 multiple of 16. */
54
127M
    Assert(STD_MAX_MATCH == 258, "Code too clever");
55
56
127M
    best_len = s->prev_length ? s->prev_length : STD_MIN_MATCH-1;
57
127M
    if (UNLIKELY(best_len >= lookahead))
58
1.01k
        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
127M
    offset = best_len >= sizeof(uint64_t) ? best_len - 7 : 0;
65
66
127M
    scan = window + strstart;
67
127M
    scan_start = zng_memread_8(scan);
68
127M
    scan_end = zng_memread_8(scan+offset);
69
127M
    mbase_end = (mbase_start+offset);
70
71
    /* Do not waste too much time if we already have a good match */
72
127M
    if (UNLIKELY(best_len >= s->good_match))
73
611k
        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
127M
    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
127M
    early_exit = s->level < EARLY_EXIT_TRIGGER_LEVEL;
114
#endif
115
0
    Assert((unsigned long)strstart <= s->window_size - MIN_LOOKAHEAD, "need lookahead");
116
143M
    for (;;) {
117
143M
        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
143M
        uint32_t len;
128
143M
        if (best_len < sizeof(uint64_t)) {
129
136M
            uint64_t cand_start = zng_memread_8(mbase_start + cur_match);
130
136M
            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
133M
                uint64_t first_mask = zng_first_bytes_mask64(best_len + 1);
135
133M
                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
133M
                if (UNLIKELY((diff & first_mask) == 0)) {
139
11.9M
                    len = zng_first_diff_byte64(diff);
140
11.9M
                    goto short_match_accept;
141
11.9M
                }
142
121M
                if (--chain_length == 0 || (cur_match = prev[cur_match & wmask]) <= limit)
143
88.7M
                    return best_len;
144
32.9M
                cand_start = zng_memread_8(mbase_start + cur_match);
145
32.9M
                if (scan_start != cand_start) {
146
                    /* Walk the remaining candidates with the chain advance kept inline. */
147
135M
                    for (;;) {
148
135M
                        diff = scan_start ^ cand_start;
149
135M
                        if (UNLIKELY((diff & first_mask) == 0)) {
150
1.07M
                            len = zng_first_diff_byte64(diff);
151
1.07M
                            goto short_match_accept;
152
1.07M
                        }
153
134M
                        if (--chain_length == 0 || (cur_match = prev[cur_match & wmask]) <= limit)
154
31.3M
                            return best_len;
155
103M
                        cand_start = zng_memread_8(mbase_start + cur_match);
156
103M
                        if (scan_start == cand_start)
157
396k
                            break;
158
103M
                    }
159
32.7M
                }
160
32.9M
            }
161
            /* All 8 bytes match, fallthrough to compare256 for the tail. */
162
136M
        } 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
71.2M
            for (;;) {
166
                /* First check the end of the candidate at best_len+1 due to the higher
167
                 * likelihood of a mismatch. */
168
71.2M
                if (zng_memcmp_8(mbase_end+cur_match, &scan_end) == 0 &&
169
5.33M
                    zng_memcmp_8(mbase_start+cur_match, &scan_start) == 0)
170
4.91M
                    break;
171
66.2M
                GOTO_NEXT_CHAIN;
172
66.2M
            }
173
7.13M
        }
174
8.46M
        len = COMPARE256(scan+2, mbase_start+cur_match+2) + 2;
175
8.46M
        Assert(scan+len <= window+(unsigned)(s->window_size-1), "wild scan");
176
177
8.46M
        if (len > best_len)
178
28.0M
short_match_accept:
179
28.0M
        {
180
28.0M
            uint32_t match_start = cur_match - match_offset;
181
28.0M
            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
28.0M
            if (UNLIKELY(len >= lookahead))
187
4.15k
                return lookahead;
188
20.5M
            if (UNLIKELY(len >= nice_match))
189
1.26M
                return len;
190
191
19.2M
            best_len = len;
192
193
19.2M
            offset = best_len >= sizeof(uint64_t) ? best_len - 7 : 0;
194
195
19.2M
            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
940k
        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
20.2M
#endif
257
20.2M
        GOTO_NEXT_CHAIN;
258
20.2M
    }
259
0
    return best_len;
260
0
}
Unexecuted instantiation: longest_match_sse2
Unexecuted instantiation: longest_match_roll_sse2
longest_match_avx2
Line
Count
Source
28
127M
Z_INTERNAL uint32_t LONGEST_MATCH(deflate_state *const s, uint32_t cur_match) {
29
127M
    const unsigned wmask = W_MASK(s);
30
127M
    unsigned int strstart = s->strstart;
31
127M
    const unsigned char *window = s->window;
32
127M
    const Pos *prev = s->prev;
33
#ifdef LONGEST_MATCH_ROLL
34
    const Pos *head = s->head;
35
#endif
36
127M
    const unsigned char *scan;
37
127M
    const unsigned char *mbase_start = window;
38
127M
    const unsigned char *mbase_end;
39
127M
    uint32_t limit;
40
#ifdef LONGEST_MATCH_ROLL
41
    uint32_t limit_base;
42
#else
43
127M
    int32_t early_exit;
44
127M
#endif
45
127M
    uint32_t chain_length = s->max_chain_length;
46
127M
    uint32_t nice_match = (uint32_t)s->nice_match;
47
127M
    uint32_t best_len, offset;
48
127M
    uint32_t lookahead = s->lookahead;
49
127M
    uint32_t match_offset = 0;
50
127M
    uint64_t scan_start;
51
127M
    uint64_t scan_end;
52
53
    /* The code is optimized for STD_MAX_MATCH-2 multiple of 16. */
54
127M
    Assert(STD_MAX_MATCH == 258, "Code too clever");
55
56
127M
    best_len = s->prev_length ? s->prev_length : STD_MIN_MATCH-1;
57
127M
    if (UNLIKELY(best_len >= lookahead))
58
1.01k
        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
127M
    offset = best_len >= sizeof(uint64_t) ? best_len - 7 : 0;
65
66
127M
    scan = window + strstart;
67
127M
    scan_start = zng_memread_8(scan);
68
127M
    scan_end = zng_memread_8(scan+offset);
69
127M
    mbase_end = (mbase_start+offset);
70
71
    /* Do not waste too much time if we already have a good match */
72
127M
    if (UNLIKELY(best_len >= s->good_match))
73
611k
        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
127M
    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
127M
    early_exit = s->level < EARLY_EXIT_TRIGGER_LEVEL;
114
127M
#endif
115
127M
    Assert((unsigned long)strstart <= s->window_size - MIN_LOOKAHEAD, "need lookahead");
116
143M
    for (;;) {
117
143M
        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
143M
        uint32_t len;
128
143M
        if (best_len < sizeof(uint64_t)) {
129
136M
            uint64_t cand_start = zng_memread_8(mbase_start + cur_match);
130
136M
            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
133M
                uint64_t first_mask = zng_first_bytes_mask64(best_len + 1);
135
133M
                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
133M
                if (UNLIKELY((diff & first_mask) == 0)) {
139
11.9M
                    len = zng_first_diff_byte64(diff);
140
11.9M
                    goto short_match_accept;
141
11.9M
                }
142
121M
                if (--chain_length == 0 || (cur_match = prev[cur_match & wmask]) <= limit)
143
88.7M
                    return best_len;
144
32.9M
                cand_start = zng_memread_8(mbase_start + cur_match);
145
32.9M
                if (scan_start != cand_start) {
146
                    /* Walk the remaining candidates with the chain advance kept inline. */
147
135M
                    for (;;) {
148
135M
                        diff = scan_start ^ cand_start;
149
135M
                        if (UNLIKELY((diff & first_mask) == 0)) {
150
1.07M
                            len = zng_first_diff_byte64(diff);
151
1.07M
                            goto short_match_accept;
152
1.07M
                        }
153
134M
                        if (--chain_length == 0 || (cur_match = prev[cur_match & wmask]) <= limit)
154
31.3M
                            return best_len;
155
103M
                        cand_start = zng_memread_8(mbase_start + cur_match);
156
103M
                        if (scan_start == cand_start)
157
396k
                            break;
158
103M
                    }
159
32.7M
                }
160
32.9M
            }
161
            /* All 8 bytes match, fallthrough to compare256 for the tail. */
162
136M
        } 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
71.2M
            for (;;) {
166
                /* First check the end of the candidate at best_len+1 due to the higher
167
                 * likelihood of a mismatch. */
168
71.2M
                if (zng_memcmp_8(mbase_end+cur_match, &scan_end) == 0 &&
169
5.33M
                    zng_memcmp_8(mbase_start+cur_match, &scan_start) == 0)
170
4.91M
                    break;
171
66.2M
                GOTO_NEXT_CHAIN;
172
66.2M
            }
173
7.13M
        }
174
8.46M
        len = COMPARE256(scan+2, mbase_start+cur_match+2) + 2;
175
8.46M
        Assert(scan+len <= window+(unsigned)(s->window_size-1), "wild scan");
176
177
8.46M
        if (len > best_len)
178
28.0M
short_match_accept:
179
28.0M
        {
180
28.0M
            uint32_t match_start = cur_match - match_offset;
181
28.0M
            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
28.0M
            if (UNLIKELY(len >= lookahead))
187
4.15k
                return lookahead;
188
20.5M
            if (UNLIKELY(len >= nice_match))
189
1.26M
                return len;
190
191
19.2M
            best_len = len;
192
193
19.2M
            offset = best_len >= sizeof(uint64_t) ? best_len - 7 : 0;
194
195
19.2M
            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
19.2M
            mbase_end = (mbase_start+offset);
248
19.2M
        }
249
940k
#ifndef LONGEST_MATCH_ROLL
250
940k
        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
20.2M
#endif
257
20.2M
        GOTO_NEXT_CHAIN;
258
20.2M
    }
259
0
    return best_len;
260
127M
}
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