Coverage Report

Created: 2025-09-04 06:50

/src/zlib-ng/match_tpl.h
Line
Count
Source (jump to first uncovered line)
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
#ifndef MATCH_TPL_H
12
#define MATCH_TPL_H
13
14
17.9M
#define EARLY_EXIT_TRIGGER_LEVEL 5
15
16
#endif
17
18
/* Set match_start to the longest match starting at the given string and
19
 * return its length. Matches shorter or equal to prev_length are discarded,
20
 * in which case the result is equal to prev_length and match_start is garbage.
21
 *
22
 * IN assertions: cur_match is the head of the hash chain for the current
23
 * string (strstart) and its distance is <= MAX_DIST, and prev_length >=1
24
 * OUT assertion: the match length is not greater than s->lookahead
25
 *
26
 * The LONGEST_MATCH_SLOW variant spends more time to attempt to find longer
27
 * matches once a match has already been found.
28
 */
29
33.5M
Z_INTERNAL uint32_t LONGEST_MATCH(deflate_state *const s, Pos cur_match) {
30
33.5M
    unsigned int strstart = s->strstart;
31
33.5M
    const unsigned wmask = s->w_mask;
32
33.5M
    unsigned char *window = s->window;
33
33.5M
    unsigned char *scan = window + strstart;
34
33.5M
    Z_REGISTER unsigned char *mbase_start = window;
35
33.5M
    Z_REGISTER unsigned char *mbase_end;
36
33.5M
    const Pos *prev = s->prev;
37
33.5M
    Pos limit;
38
#ifdef LONGEST_MATCH_SLOW
39
    Pos limit_base;
40
#else
41
    int32_t early_exit;
42
#endif
43
33.5M
    uint32_t chain_length, nice_match, best_len, offset;
44
33.5M
    uint32_t lookahead = s->lookahead;
45
33.5M
    Pos match_offset = 0;
46
33.5M
    uint64_t scan_start;
47
33.5M
    uint64_t scan_end;
48
49
33.5M
#define GOTO_NEXT_CHAIN \
50
255M
    if (--chain_length && (cur_match = prev[cur_match & wmask]) > limit) \
51
224M
        continue; \
52
31.3M
    return best_len;
53
54
    /* The code is optimized for STD_MAX_MATCH-2 multiple of 16. */
55
33.5M
    Assert(STD_MAX_MATCH == 258, "Code too clever");
56
57
33.5M
    best_len = s->prev_length ? s->prev_length : STD_MIN_MATCH-1;
58
59
    /* Calculate read offset which should only extend an extra byte
60
     * to find the next best match length.
61
     */
62
33.5M
    offset = best_len-1;
63
33.5M
    if (best_len >= sizeof(uint32_t)) {
64
3.05M
        offset -= 2;
65
3.05M
        if (best_len >= sizeof(uint64_t))
66
801k
            offset -= 4;
67
3.05M
    }
68
69
33.5M
    scan_start = zng_memread_8(scan);
70
33.5M
    scan_end = zng_memread_8(scan+offset);
71
33.5M
    mbase_end  = (mbase_start+offset);
72
73
    /* Do not waste too much time if we already have a good match */
74
33.5M
    chain_length = s->max_chain_length;
75
33.5M
    if (best_len >= s->good_match)
76
526k
        chain_length >>= 2;
77
33.5M
    nice_match = (uint32_t)s->nice_match;
78
79
    /* Stop when cur_match becomes <= limit. To simplify the code,
80
     * we prevent matches with the string of window index 0
81
     */
82
33.5M
    limit = strstart > MAX_DIST(s) ? (Pos)(strstart - MAX_DIST(s)) : 0;
83
#ifdef LONGEST_MATCH_SLOW
84
    limit_base = limit;
85
15.5M
    if (best_len >= STD_MIN_MATCH) {
86
        /* We're continuing search (lazy evaluation). */
87
3.15M
        uint32_t i, hash;
88
3.15M
        Pos pos;
89
90
        /* Find a most distant chain starting from scan with index=1 (index=0 corresponds
91
         * to cur_match). We cannot use s->prev[strstart+1,...] immediately, because
92
         * these strings are not yet inserted into the hash table.
93
         */
94
        hash = s->update_hash(0, scan[1]);
95
        hash = s->update_hash(hash, scan[2]);
96
97
14.8M
        for (i = 3; i <= best_len; i++) {
98
11.6M
            hash = s->update_hash(hash, scan[i]);
99
100
            /* If we're starting with best_len >= 3, we can use offset search. */
101
11.6M
            pos = s->head[hash];
102
11.6M
            if (pos < cur_match) {
103
2.80M
                match_offset = (Pos)(i - 2);
104
2.80M
                cur_match = pos;
105
2.80M
            }
106
11.6M
        }
107
108
        /* Update offset-dependent variables */
109
3.15M
        limit = limit_base+match_offset;
110
3.15M
        if (cur_match <= limit)
111
679k
            goto break_matching;
112
2.47M
        mbase_start -= match_offset;
113
2.47M
        mbase_end -= match_offset;
114
2.47M
    }
115
#else
116
17.9M
    early_exit = s->level < EARLY_EXIT_TRIGGER_LEVEL;
117
#endif
118
14.8M
    Assert((unsigned long)strstart <= s->window_size - MIN_LOOKAHEAD, "need lookahead");
119
44.1M
    for (;;) {
120
44.1M
        if (cur_match >= strstart)
121
26
            break;
122
123
        /* Skip to next match if the match length cannot increase or if the match length is
124
         * less than 2. Note that the checks below for insufficient lookahead only occur
125
         * occasionally for performance reasons.
126
         * Therefore uninitialized memory will be accessed and conditional jumps will be made
127
         * that depend on those values. However the length of the match is limited to the
128
         * lookahead, so the output of deflate is not affected by the uninitialized values.
129
         */
130
44.1M
        if (best_len < sizeof(uint32_t)) {
131
74.9M
            for (;;) {
132
74.9M
                if (zng_memcmp_2(mbase_end+cur_match, &scan_end) == 0 &&
133
74.9M
                    zng_memcmp_2(mbase_start+cur_match, &scan_start) == 0)
134
10.6M
                    break;
135
64.3M
                GOTO_NEXT_CHAIN;
136
64.3M
            }
137
33.1M
        } else if (best_len >= sizeof(uint64_t)) {
138
80.4M
            for (;;) {
139
80.4M
                if (zng_memcmp_8(mbase_end+cur_match, &scan_end) == 0 &&
140
80.4M
                    zng_memcmp_8(mbase_start+cur_match, &scan_start) == 0)
141
2.67M
                    break;
142
77.7M
                GOTO_NEXT_CHAIN;
143
77.7M
            }
144
6.61M
        } else {
145
104M
            for (;;) {
146
104M
                if (zng_memcmp_4(mbase_end+cur_match, &scan_end) == 0 &&
147
104M
                    zng_memcmp_4(mbase_start+cur_match, &scan_start) == 0)
148
1.68M
                    break;
149
102M
                GOTO_NEXT_CHAIN;
150
102M
            }
151
6.61M
        }
152
15.0M
        uint32_t len = COMPARE256(scan+2, mbase_start+cur_match+2) + 2;
153
15.0M
        Assert(scan+len <= window+(unsigned)(s->window_size-1), "wild scan");
154
155
15.0M
        if (len > best_len) {
156
13.9M
            uint32_t match_start = cur_match - match_offset;
157
13.9M
            s->match_start = match_start;
158
159
            /* Do not look for matches beyond the end of the input. */
160
13.9M
            if (len > lookahead)
161
829
                return lookahead;
162
13.9M
            best_len = len;
163
13.9M
            if (best_len >= nice_match)
164
682k
                return best_len;
165
166
13.3M
            offset = best_len-1;
167
13.3M
            if (best_len >= sizeof(uint32_t)) {
168
10.0M
                offset -= 2;
169
10.0M
                if (best_len >= sizeof(uint64_t))
170
3.11M
                    offset -= 4;
171
10.0M
            }
172
173
13.3M
            scan_end = zng_memread_8(scan+offset);
174
175
#ifdef LONGEST_MATCH_SLOW
176
            /* Look for a better string offset */
177
6.33M
            if (UNLIKELY(len > STD_MIN_MATCH && match_start + len < strstart)) {
178
2.97M
                Pos pos, next_pos;
179
2.97M
                uint32_t i, hash;
180
2.97M
                unsigned char *scan_endstr;
181
182
                /* Go back to offset 0 */
183
                cur_match -= match_offset;
184
                match_offset = 0;
185
                next_pos = cur_match;
186
40.5M
                for (i = 0; i <= len - STD_MIN_MATCH; i++) {
187
38.4M
                    pos = prev[(cur_match + i) & wmask];
188
38.4M
                    if (pos < next_pos) {
189
                        /* Hash chain is more distant, use it */
190
4.09M
                        if (pos <= limit_base + i)
191
804k
                            goto break_matching;
192
3.28M
                        next_pos = pos;
193
3.28M
                        match_offset = (Pos)i;
194
3.28M
                    }
195
38.4M
                }
196
                /* Switch cur_match to next_pos chain */
197
2.16M
                cur_match = next_pos;
198
199
                /* Try hash head at len-(STD_MIN_MATCH-1) position to see if we could get
200
                 * a better cur_match at the end of string. Using (STD_MIN_MATCH-1) lets
201
                 * us include one more byte into hash - the byte which will be checked
202
                 * in main loop now, and which allows to grow match by 1.
203
                 */
204
2.16M
                scan_endstr = scan + len - (STD_MIN_MATCH+1);
205
206
2.16M
                hash = s->update_hash(0, scan_endstr[0]);
207
2.16M
                hash = s->update_hash(hash, scan_endstr[1]);
208
2.16M
                hash = s->update_hash(hash, scan_endstr[2]);
209
210
2.16M
                pos = s->head[hash];
211
2.16M
                if (pos < cur_match) {
212
0
                    match_offset = (Pos)(len - (STD_MIN_MATCH+1));
213
0
                    if (pos <= limit_base + match_offset)
214
0
                        goto break_matching;
215
0
                    cur_match = pos;
216
0
                }
217
218
                /* Update offset-dependent variables */
219
2.16M
                limit = limit_base+match_offset;
220
2.16M
                mbase_start = window-match_offset;
221
2.16M
                mbase_end = (mbase_start+offset);
222
2.16M
                continue;
223
2.16M
            }
224
3.36M
#endif
225
3.36M
            mbase_end = (mbase_start+offset);
226
3.36M
        }
227
#ifndef LONGEST_MATCH_SLOW
228
110k
        else if (UNLIKELY(early_exit)) {
229
            /* The probability of finding a match later if we here is pretty low, so for
230
             * performance it's best to outright stop here for the lower compression levels
231
             */
232
590
            break;
233
590
        }
234
7.08M
#endif
235
11.3M
        GOTO_NEXT_CHAIN;
236
11.3M
    }
237
616
    return best_len;
238
239
#ifdef LONGEST_MATCH_SLOW
240
1.48M
break_matching:
241
242
1.48M
    if (best_len < s->lookahead)
243
1.48M
        return best_len;
244
245
210
    return s->lookahead;
246
#endif
247
1.48M
}
Unexecuted instantiation: longest_match_sse2
Unexecuted instantiation: longest_match_slow_sse2
longest_match_avx2
Line
Count
Source
29
17.9M
Z_INTERNAL uint32_t LONGEST_MATCH(deflate_state *const s, Pos cur_match) {
30
17.9M
    unsigned int strstart = s->strstart;
31
17.9M
    const unsigned wmask = s->w_mask;
32
17.9M
    unsigned char *window = s->window;
33
17.9M
    unsigned char *scan = window + strstart;
34
17.9M
    Z_REGISTER unsigned char *mbase_start = window;
35
17.9M
    Z_REGISTER unsigned char *mbase_end;
36
17.9M
    const Pos *prev = s->prev;
37
17.9M
    Pos limit;
38
#ifdef LONGEST_MATCH_SLOW
39
    Pos limit_base;
40
#else
41
17.9M
    int32_t early_exit;
42
17.9M
#endif
43
17.9M
    uint32_t chain_length, nice_match, best_len, offset;
44
17.9M
    uint32_t lookahead = s->lookahead;
45
17.9M
    Pos match_offset = 0;
46
17.9M
    uint64_t scan_start;
47
17.9M
    uint64_t scan_end;
48
49
17.9M
#define GOTO_NEXT_CHAIN \
50
17.9M
    if (--chain_length && (cur_match = prev[cur_match & wmask]) > limit) \
51
17.9M
        continue; \
52
17.9M
    return best_len;
53
54
    /* The code is optimized for STD_MAX_MATCH-2 multiple of 16. */
55
17.9M
    Assert(STD_MAX_MATCH == 258, "Code too clever");
56
57
17.9M
    best_len = s->prev_length ? s->prev_length : STD_MIN_MATCH-1;
58
59
    /* Calculate read offset which should only extend an extra byte
60
     * to find the next best match length.
61
     */
62
17.9M
    offset = best_len-1;
63
17.9M
    if (best_len >= sizeof(uint32_t)) {
64
1.16M
        offset -= 2;
65
1.16M
        if (best_len >= sizeof(uint64_t))
66
490k
            offset -= 4;
67
1.16M
    }
68
69
17.9M
    scan_start = zng_memread_8(scan);
70
17.9M
    scan_end = zng_memread_8(scan+offset);
71
17.9M
    mbase_end  = (mbase_start+offset);
72
73
    /* Do not waste too much time if we already have a good match */
74
17.9M
    chain_length = s->max_chain_length;
75
17.9M
    if (best_len >= s->good_match)
76
477k
        chain_length >>= 2;
77
17.9M
    nice_match = (uint32_t)s->nice_match;
78
79
    /* Stop when cur_match becomes <= limit. To simplify the code,
80
     * we prevent matches with the string of window index 0
81
     */
82
17.9M
    limit = strstart > MAX_DIST(s) ? (Pos)(strstart - MAX_DIST(s)) : 0;
83
#ifdef LONGEST_MATCH_SLOW
84
    limit_base = limit;
85
    if (best_len >= STD_MIN_MATCH) {
86
        /* We're continuing search (lazy evaluation). */
87
        uint32_t i, hash;
88
        Pos pos;
89
90
        /* Find a most distant chain starting from scan with index=1 (index=0 corresponds
91
         * to cur_match). We cannot use s->prev[strstart+1,...] immediately, because
92
         * these strings are not yet inserted into the hash table.
93
         */
94
        hash = s->update_hash(0, scan[1]);
95
        hash = s->update_hash(hash, scan[2]);
96
97
        for (i = 3; i <= best_len; i++) {
98
            hash = s->update_hash(hash, scan[i]);
99
100
            /* If we're starting with best_len >= 3, we can use offset search. */
101
            pos = s->head[hash];
102
            if (pos < cur_match) {
103
                match_offset = (Pos)(i - 2);
104
                cur_match = pos;
105
            }
106
        }
107
108
        /* Update offset-dependent variables */
109
        limit = limit_base+match_offset;
110
        if (cur_match <= limit)
111
            goto break_matching;
112
        mbase_start -= match_offset;
113
        mbase_end -= match_offset;
114
    }
115
#else
116
17.9M
    early_exit = s->level < EARLY_EXIT_TRIGGER_LEVEL;
117
17.9M
#endif
118
17.9M
    Assert((unsigned long)strstart <= s->window_size - MIN_LOOKAHEAD, "need lookahead");
119
23.2M
    for (;;) {
120
23.2M
        if (cur_match >= strstart)
121
26
            break;
122
123
        /* Skip to next match if the match length cannot increase or if the match length is
124
         * less than 2. Note that the checks below for insufficient lookahead only occur
125
         * occasionally for performance reasons.
126
         * Therefore uninitialized memory will be accessed and conditional jumps will be made
127
         * that depend on those values. However the length of the match is limited to the
128
         * lookahead, so the output of deflate is not affected by the uninitialized values.
129
         */
130
23.2M
        if (best_len < sizeof(uint32_t)) {
131
21.2M
            for (;;) {
132
21.2M
                if (zng_memcmp_2(mbase_end+cur_match, &scan_end) == 0 &&
133
21.2M
                    zng_memcmp_2(mbase_start+cur_match, &scan_start) == 0)
134
5.18M
                    break;
135
16.0M
                GOTO_NEXT_CHAIN;
136
16.0M
            }
137
16.8M
        } else if (best_len >= sizeof(uint64_t)) {
138
48.0M
            for (;;) {
139
48.0M
                if (zng_memcmp_8(mbase_end+cur_match, &scan_end) == 0 &&
140
48.0M
                    zng_memcmp_8(mbase_start+cur_match, &scan_start) == 0)
141
1.29M
                    break;
142
46.7M
                GOTO_NEXT_CHAIN;
143
46.7M
            }
144
3.86M
        } else {
145
74.1M
            for (;;) {
146
74.1M
                if (zng_memcmp_4(mbase_end+cur_match, &scan_end) == 0 &&
147
74.1M
                    zng_memcmp_4(mbase_start+cur_match, &scan_start) == 0)
148
1.26M
                    break;
149
72.9M
                GOTO_NEXT_CHAIN;
150
72.9M
            }
151
3.86M
        }
152
7.73M
        uint32_t len = COMPARE256(scan+2, mbase_start+cur_match+2) + 2;
153
7.73M
        Assert(scan+len <= window+(unsigned)(s->window_size-1), "wild scan");
154
155
7.73M
        if (len > best_len) {
156
7.62M
            uint32_t match_start = cur_match - match_offset;
157
7.62M
            s->match_start = match_start;
158
159
            /* Do not look for matches beyond the end of the input. */
160
7.62M
            if (len > lookahead)
161
453
                return lookahead;
162
7.62M
            best_len = len;
163
7.62M
            if (best_len >= nice_match)
164
648k
                return best_len;
165
166
6.97M
            offset = best_len-1;
167
6.97M
            if (best_len >= sizeof(uint32_t)) {
168
6.97M
                offset -= 2;
169
6.97M
                if (best_len >= sizeof(uint64_t))
170
2.35M
                    offset -= 4;
171
6.97M
            }
172
173
6.97M
            scan_end = zng_memread_8(scan+offset);
174
175
#ifdef LONGEST_MATCH_SLOW
176
            /* Look for a better string offset */
177
            if (UNLIKELY(len > STD_MIN_MATCH && match_start + len < strstart)) {
178
                Pos pos, next_pos;
179
                uint32_t i, hash;
180
                unsigned char *scan_endstr;
181
182
                /* Go back to offset 0 */
183
                cur_match -= match_offset;
184
                match_offset = 0;
185
                next_pos = cur_match;
186
                for (i = 0; i <= len - STD_MIN_MATCH; i++) {
187
                    pos = prev[(cur_match + i) & wmask];
188
                    if (pos < next_pos) {
189
                        /* Hash chain is more distant, use it */
190
                        if (pos <= limit_base + i)
191
                            goto break_matching;
192
                        next_pos = pos;
193
                        match_offset = (Pos)i;
194
                    }
195
                }
196
                /* Switch cur_match to next_pos chain */
197
                cur_match = next_pos;
198
199
                /* Try hash head at len-(STD_MIN_MATCH-1) position to see if we could get
200
                 * a better cur_match at the end of string. Using (STD_MIN_MATCH-1) lets
201
                 * us include one more byte into hash - the byte which will be checked
202
                 * in main loop now, and which allows to grow match by 1.
203
                 */
204
                scan_endstr = scan + len - (STD_MIN_MATCH+1);
205
206
                hash = s->update_hash(0, scan_endstr[0]);
207
                hash = s->update_hash(hash, scan_endstr[1]);
208
                hash = s->update_hash(hash, scan_endstr[2]);
209
210
                pos = s->head[hash];
211
                if (pos < cur_match) {
212
                    match_offset = (Pos)(len - (STD_MIN_MATCH+1));
213
                    if (pos <= limit_base + match_offset)
214
                        goto break_matching;
215
                    cur_match = pos;
216
                }
217
218
                /* Update offset-dependent variables */
219
                limit = limit_base+match_offset;
220
                mbase_start = window-match_offset;
221
                mbase_end = (mbase_start+offset);
222
                continue;
223
            }
224
#endif
225
6.97M
            mbase_end = (mbase_start+offset);
226
6.97M
        }
227
110k
#ifndef LONGEST_MATCH_SLOW
228
110k
        else if (UNLIKELY(early_exit)) {
229
            /* The probability of finding a match later if we here is pretty low, so for
230
             * performance it's best to outright stop here for the lower compression levels
231
             */
232
590
            break;
233
590
        }
234
7.08M
#endif
235
7.08M
        GOTO_NEXT_CHAIN;
236
7.08M
    }
237
616
    return best_len;
238
239
#ifdef LONGEST_MATCH_SLOW
240
break_matching:
241
242
    if (best_len < s->lookahead)
243
        return best_len;
244
245
    return s->lookahead;
246
#endif
247
17.9M
}
longest_match_slow_avx2
Line
Count
Source
29
15.5M
Z_INTERNAL uint32_t LONGEST_MATCH(deflate_state *const s, Pos cur_match) {
30
15.5M
    unsigned int strstart = s->strstart;
31
15.5M
    const unsigned wmask = s->w_mask;
32
15.5M
    unsigned char *window = s->window;
33
15.5M
    unsigned char *scan = window + strstart;
34
15.5M
    Z_REGISTER unsigned char *mbase_start = window;
35
15.5M
    Z_REGISTER unsigned char *mbase_end;
36
15.5M
    const Pos *prev = s->prev;
37
15.5M
    Pos limit;
38
15.5M
#ifdef LONGEST_MATCH_SLOW
39
15.5M
    Pos limit_base;
40
#else
41
    int32_t early_exit;
42
#endif
43
15.5M
    uint32_t chain_length, nice_match, best_len, offset;
44
15.5M
    uint32_t lookahead = s->lookahead;
45
15.5M
    Pos match_offset = 0;
46
15.5M
    uint64_t scan_start;
47
15.5M
    uint64_t scan_end;
48
49
15.5M
#define GOTO_NEXT_CHAIN \
50
15.5M
    if (--chain_length && (cur_match = prev[cur_match & wmask]) > limit) \
51
15.5M
        continue; \
52
15.5M
    return best_len;
53
54
    /* The code is optimized for STD_MAX_MATCH-2 multiple of 16. */
55
15.5M
    Assert(STD_MAX_MATCH == 258, "Code too clever");
56
57
15.5M
    best_len = s->prev_length ? s->prev_length : STD_MIN_MATCH-1;
58
59
    /* Calculate read offset which should only extend an extra byte
60
     * to find the next best match length.
61
     */
62
15.5M
    offset = best_len-1;
63
15.5M
    if (best_len >= sizeof(uint32_t)) {
64
1.88M
        offset -= 2;
65
1.88M
        if (best_len >= sizeof(uint64_t))
66
310k
            offset -= 4;
67
1.88M
    }
68
69
15.5M
    scan_start = zng_memread_8(scan);
70
15.5M
    scan_end = zng_memread_8(scan+offset);
71
15.5M
    mbase_end  = (mbase_start+offset);
72
73
    /* Do not waste too much time if we already have a good match */
74
15.5M
    chain_length = s->max_chain_length;
75
15.5M
    if (best_len >= s->good_match)
76
48.8k
        chain_length >>= 2;
77
15.5M
    nice_match = (uint32_t)s->nice_match;
78
79
    /* Stop when cur_match becomes <= limit. To simplify the code,
80
     * we prevent matches with the string of window index 0
81
     */
82
15.5M
    limit = strstart > MAX_DIST(s) ? (Pos)(strstart - MAX_DIST(s)) : 0;
83
15.5M
#ifdef LONGEST_MATCH_SLOW
84
15.5M
    limit_base = limit;
85
15.5M
    if (best_len >= STD_MIN_MATCH) {
86
        /* We're continuing search (lazy evaluation). */
87
3.15M
        uint32_t i, hash;
88
3.15M
        Pos pos;
89
90
        /* Find a most distant chain starting from scan with index=1 (index=0 corresponds
91
         * to cur_match). We cannot use s->prev[strstart+1,...] immediately, because
92
         * these strings are not yet inserted into the hash table.
93
         */
94
3.15M
        hash = s->update_hash(0, scan[1]);
95
3.15M
        hash = s->update_hash(hash, scan[2]);
96
97
14.8M
        for (i = 3; i <= best_len; i++) {
98
11.6M
            hash = s->update_hash(hash, scan[i]);
99
100
            /* If we're starting with best_len >= 3, we can use offset search. */
101
11.6M
            pos = s->head[hash];
102
11.6M
            if (pos < cur_match) {
103
2.80M
                match_offset = (Pos)(i - 2);
104
2.80M
                cur_match = pos;
105
2.80M
            }
106
11.6M
        }
107
108
        /* Update offset-dependent variables */
109
3.15M
        limit = limit_base+match_offset;
110
3.15M
        if (cur_match <= limit)
111
679k
            goto break_matching;
112
2.47M
        mbase_start -= match_offset;
113
2.47M
        mbase_end -= match_offset;
114
2.47M
    }
115
#else
116
    early_exit = s->level < EARLY_EXIT_TRIGGER_LEVEL;
117
#endif
118
14.8M
    Assert((unsigned long)strstart <= s->window_size - MIN_LOOKAHEAD, "need lookahead");
119
20.8M
    for (;;) {
120
20.8M
        if (cur_match >= strstart)
121
0
            break;
122
123
        /* Skip to next match if the match length cannot increase or if the match length is
124
         * less than 2. Note that the checks below for insufficient lookahead only occur
125
         * occasionally for performance reasons.
126
         * Therefore uninitialized memory will be accessed and conditional jumps will be made
127
         * that depend on those values. However the length of the match is limited to the
128
         * lookahead, so the output of deflate is not affected by the uninitialized values.
129
         */
130
20.8M
        if (best_len < sizeof(uint32_t)) {
131
53.7M
            for (;;) {
132
53.7M
                if (zng_memcmp_2(mbase_end+cur_match, &scan_end) == 0 &&
133
53.7M
                    zng_memcmp_2(mbase_start+cur_match, &scan_start) == 0)
134
5.45M
                    break;
135
48.2M
                GOTO_NEXT_CHAIN;
136
48.2M
            }
137
16.3M
        } else if (best_len >= sizeof(uint64_t)) {
138
32.4M
            for (;;) {
139
32.4M
                if (zng_memcmp_8(mbase_end+cur_match, &scan_end) == 0 &&
140
32.4M
                    zng_memcmp_8(mbase_start+cur_match, &scan_start) == 0)
141
1.38M
                    break;
142
31.0M
                GOTO_NEXT_CHAIN;
143
31.0M
            }
144
2.75M
        } else {
145
30.0M
            for (;;) {
146
30.0M
                if (zng_memcmp_4(mbase_end+cur_match, &scan_end) == 0 &&
147
30.0M
                    zng_memcmp_4(mbase_start+cur_match, &scan_start) == 0)
148
424k
                    break;
149
29.6M
                GOTO_NEXT_CHAIN;
150
29.6M
            }
151
2.75M
        }
152
7.26M
        uint32_t len = COMPARE256(scan+2, mbase_start+cur_match+2) + 2;
153
7.26M
        Assert(scan+len <= window+(unsigned)(s->window_size-1), "wild scan");
154
155
7.26M
        if (len > best_len) {
156
6.36M
            uint32_t match_start = cur_match - match_offset;
157
6.36M
            s->match_start = match_start;
158
159
            /* Do not look for matches beyond the end of the input. */
160
6.36M
            if (len > lookahead)
161
376
                return lookahead;
162
6.36M
            best_len = len;
163
6.36M
            if (best_len >= nice_match)
164
33.7k
                return best_len;
165
166
6.33M
            offset = best_len-1;
167
6.33M
            if (best_len >= sizeof(uint32_t)) {
168
3.05M
                offset -= 2;
169
3.05M
                if (best_len >= sizeof(uint64_t))
170
768k
                    offset -= 4;
171
3.05M
            }
172
173
6.33M
            scan_end = zng_memread_8(scan+offset);
174
175
6.33M
#ifdef LONGEST_MATCH_SLOW
176
            /* Look for a better string offset */
177
6.33M
            if (UNLIKELY(len > STD_MIN_MATCH && match_start + len < strstart)) {
178
2.97M
                Pos pos, next_pos;
179
2.97M
                uint32_t i, hash;
180
2.97M
                unsigned char *scan_endstr;
181
182
                /* Go back to offset 0 */
183
2.97M
                cur_match -= match_offset;
184
2.97M
                match_offset = 0;
185
2.97M
                next_pos = cur_match;
186
40.5M
                for (i = 0; i <= len - STD_MIN_MATCH; i++) {
187
38.4M
                    pos = prev[(cur_match + i) & wmask];
188
38.4M
                    if (pos < next_pos) {
189
                        /* Hash chain is more distant, use it */
190
4.09M
                        if (pos <= limit_base + i)
191
804k
                            goto break_matching;
192
3.28M
                        next_pos = pos;
193
3.28M
                        match_offset = (Pos)i;
194
3.28M
                    }
195
38.4M
                }
196
                /* Switch cur_match to next_pos chain */
197
2.16M
                cur_match = next_pos;
198
199
                /* Try hash head at len-(STD_MIN_MATCH-1) position to see if we could get
200
                 * a better cur_match at the end of string. Using (STD_MIN_MATCH-1) lets
201
                 * us include one more byte into hash - the byte which will be checked
202
                 * in main loop now, and which allows to grow match by 1.
203
                 */
204
2.16M
                scan_endstr = scan + len - (STD_MIN_MATCH+1);
205
206
2.16M
                hash = s->update_hash(0, scan_endstr[0]);
207
2.16M
                hash = s->update_hash(hash, scan_endstr[1]);
208
2.16M
                hash = s->update_hash(hash, scan_endstr[2]);
209
210
2.16M
                pos = s->head[hash];
211
2.16M
                if (pos < cur_match) {
212
0
                    match_offset = (Pos)(len - (STD_MIN_MATCH+1));
213
0
                    if (pos <= limit_base + match_offset)
214
0
                        goto break_matching;
215
0
                    cur_match = pos;
216
0
                }
217
218
                /* Update offset-dependent variables */
219
2.16M
                limit = limit_base+match_offset;
220
2.16M
                mbase_start = window-match_offset;
221
2.16M
                mbase_end = (mbase_start+offset);
222
2.16M
                continue;
223
2.16M
            }
224
3.36M
#endif
225
3.36M
            mbase_end = (mbase_start+offset);
226
3.36M
        }
227
#ifndef LONGEST_MATCH_SLOW
228
        else if (UNLIKELY(early_exit)) {
229
            /* The probability of finding a match later if we here is pretty low, so for
230
             * performance it's best to outright stop here for the lower compression levels
231
             */
232
            break;
233
        }
234
#endif
235
4.25M
        GOTO_NEXT_CHAIN;
236
4.25M
    }
237
0
    return best_len;
238
239
0
#ifdef LONGEST_MATCH_SLOW
240
1.48M
break_matching:
241
242
1.48M
    if (best_len < s->lookahead)
243
1.48M
        return best_len;
244
245
210
    return s->lookahead;
246
1.48M
#endif
247
1.48M
}
Unexecuted instantiation: longest_match_avx512
Unexecuted instantiation: longest_match_slow_avx512
248
249
#undef LONGEST_MATCH_SLOW
250
#undef LONGEST_MATCH