Coverage Report

Created: 2025-07-11 06:15

/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
222M
#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
241M
Z_INTERNAL uint32_t LONGEST_MATCH(deflate_state *const s, Pos cur_match) {
30
241M
    unsigned int strstart = s->strstart;
31
241M
    const unsigned wmask = s->w_mask;
32
241M
    unsigned char *window = s->window;
33
241M
    unsigned char *scan = window + strstart;
34
241M
    Z_REGISTER unsigned char *mbase_start = window;
35
241M
    Z_REGISTER unsigned char *mbase_end;
36
241M
    const Pos *prev = s->prev;
37
241M
    Pos limit;
38
#ifdef LONGEST_MATCH_SLOW
39
    Pos limit_base;
40
#else
41
    int32_t early_exit;
42
#endif
43
241M
    uint32_t chain_length, nice_match, best_len, offset;
44
241M
    uint32_t lookahead = s->lookahead;
45
241M
    Pos match_offset = 0;
46
241M
    uint64_t scan_start;
47
241M
    uint64_t scan_end;
48
49
241M
#define GOTO_NEXT_CHAIN \
50
1.07G
    if (--chain_length && (cur_match = prev[cur_match & wmask]) > limit) \
51
835M
        continue; \
52
236M
    return best_len;
53
54
    /* The code is optimized for STD_MAX_MATCH-2 multiple of 16. */
55
241M
    Assert(STD_MAX_MATCH == 258, "Code too clever");
56
57
241M
    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
241M
    offset = best_len-1;
63
241M
    if (best_len >= sizeof(uint32_t)) {
64
7.11M
        offset -= 2;
65
7.11M
        if (best_len >= sizeof(uint64_t))
66
2.44M
            offset -= 4;
67
7.11M
    }
68
69
241M
    scan_start = zng_memread_8(scan);
70
241M
    scan_end = zng_memread_8(scan+offset);
71
241M
    mbase_end  = (mbase_start+offset);
72
73
    /* Do not waste too much time if we already have a good match */
74
241M
    chain_length = s->max_chain_length;
75
241M
    if (best_len >= s->good_match)
76
2.01M
        chain_length >>= 2;
77
241M
    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
241M
    limit = strstart > MAX_DIST(s) ? (Pos)(strstart - MAX_DIST(s)) : 0;
83
#ifdef LONGEST_MATCH_SLOW
84
    limit_base = limit;
85
19.4M
    if (best_len >= STD_MIN_MATCH) {
86
        /* We're continuing search (lazy evaluation). */
87
3.78M
        uint32_t i, hash;
88
3.78M
        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
20.4M
        for (i = 3; i <= best_len; i++) {
98
16.6M
            hash = s->update_hash(hash, scan[i]);
99
100
            /* If we're starting with best_len >= 3, we can use offset search. */
101
16.6M
            pos = s->head[hash];
102
16.6M
            if (pos < cur_match) {
103
3.46M
                match_offset = (Pos)(i - 2);
104
3.46M
                cur_match = pos;
105
3.46M
            }
106
16.6M
        }
107
108
        /* Update offset-dependent variables */
109
3.78M
        limit = limit_base+match_offset;
110
3.78M
        if (cur_match <= limit)
111
902k
            goto break_matching;
112
2.88M
        mbase_start -= match_offset;
113
2.88M
        mbase_end -= match_offset;
114
2.88M
    }
115
#else
116
222M
    early_exit = s->level < EARLY_EXIT_TRIGGER_LEVEL;
117
#endif
118
18.5M
    Assert((unsigned long)strstart <= s->window_size - MIN_LOOKAHEAD, "need lookahead");
119
284M
    for (;;) {
120
284M
        if (cur_match >= strstart)
121
44
            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
284M
        if (best_len < sizeof(uint32_t)) {
131
350M
            for (;;) {
132
350M
                if (zng_memcmp_2(mbase_end+cur_match, &scan_end) == 0 &&
133
350M
                    zng_memcmp_2(mbase_start+cur_match, &scan_start) == 0)
134
37.6M
                    break;
135
312M
                GOTO_NEXT_CHAIN;
136
312M
            }
137
237M
        } else if (best_len >= sizeof(uint64_t)) {
138
323M
            for (;;) {
139
323M
                if (zng_memcmp_8(mbase_end+cur_match, &scan_end) == 0 &&
140
323M
                    zng_memcmp_8(mbase_start+cur_match, &scan_start) == 0)
141
11.3M
                    break;
142
311M
                GOTO_NEXT_CHAIN;
143
311M
            }
144
26.7M
        } else {
145
406M
            for (;;) {
146
406M
                if (zng_memcmp_4(mbase_end+cur_match, &scan_end) == 0 &&
147
406M
                    zng_memcmp_4(mbase_start+cur_match, &scan_start) == 0)
148
9.17M
                    break;
149
397M
                GOTO_NEXT_CHAIN;
150
397M
            }
151
26.7M
        }
152
58.1M
        uint32_t len = COMPARE256(scan+2, mbase_start+cur_match+2) + 2;
153
58.1M
        Assert(scan+len <= window+(unsigned)(s->window_size-1), "wild scan");
154
155
58.1M
        if (len > best_len) {
156
55.7M
            uint32_t match_start = cur_match - match_offset;
157
55.7M
            s->match_start = match_start;
158
159
            /* Do not look for matches beyond the end of the input. */
160
55.7M
            if (len > lookahead)
161
8.98k
                return lookahead;
162
55.7M
            best_len = len;
163
55.7M
            if (best_len >= nice_match)
164
3.71M
                return best_len;
165
166
52.0M
            offset = best_len-1;
167
52.0M
            if (best_len >= sizeof(uint32_t)) {
168
48.3M
                offset -= 2;
169
48.3M
                if (best_len >= sizeof(uint64_t))
170
17.5M
                    offset -= 4;
171
48.3M
            }
172
173
52.0M
            scan_end = zng_memread_8(scan+offset);
174
175
#ifdef LONGEST_MATCH_SLOW
176
            /* Look for a better string offset */
177
8.08M
            if (UNLIKELY(len > STD_MIN_MATCH && match_start + len < strstart)) {
178
4.25M
                Pos pos, next_pos;
179
4.25M
                uint32_t i, hash;
180
4.25M
                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
104M
                for (i = 0; i <= len - STD_MIN_MATCH; i++) {
187
101M
                    pos = prev[(cur_match + i) & wmask];
188
101M
                    if (pos < next_pos) {
189
                        /* Hash chain is more distant, use it */
190
5.65M
                        if (pos <= limit_base + i)
191
977k
                            goto break_matching;
192
4.67M
                        next_pos = pos;
193
4.67M
                        match_offset = (Pos)i;
194
4.67M
                    }
195
101M
                }
196
                /* Switch cur_match to next_pos chain */
197
3.27M
                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
3.27M
                scan_endstr = scan + len - (STD_MIN_MATCH+1);
205
206
3.27M
                hash = s->update_hash(0, scan_endstr[0]);
207
3.27M
                hash = s->update_hash(hash, scan_endstr[1]);
208
3.27M
                hash = s->update_hash(hash, scan_endstr[2]);
209
210
3.27M
                pos = s->head[hash];
211
3.27M
                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
3.27M
                limit = limit_base+match_offset;
220
3.27M
                mbase_start = window-match_offset;
221
3.27M
                mbase_end = (mbase_start+offset);
222
3.27M
                continue;
223
3.27M
            }
224
3.82M
#endif
225
3.82M
            mbase_end = (mbase_start+offset);
226
3.82M
        }
227
#ifndef LONGEST_MATCH_SLOW
228
1.04M
        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
778
            break;
233
778
        }
234
44.9M
#endif
235
50.1M
        GOTO_NEXT_CHAIN;
236
50.1M
    }
237
822
    return best_len;
238
239
#ifdef LONGEST_MATCH_SLOW
240
1.88M
break_matching:
241
242
1.88M
    if (best_len < s->lookahead)
243
1.87M
        return best_len;
244
245
1.26k
    return s->lookahead;
246
#endif
247
1.88M
}
Unexecuted instantiation: longest_match_sse2
Unexecuted instantiation: longest_match_slow_sse2
longest_match_avx2
Line
Count
Source
29
222M
Z_INTERNAL uint32_t LONGEST_MATCH(deflate_state *const s, Pos cur_match) {
30
222M
    unsigned int strstart = s->strstart;
31
222M
    const unsigned wmask = s->w_mask;
32
222M
    unsigned char *window = s->window;
33
222M
    unsigned char *scan = window + strstart;
34
222M
    Z_REGISTER unsigned char *mbase_start = window;
35
222M
    Z_REGISTER unsigned char *mbase_end;
36
222M
    const Pos *prev = s->prev;
37
222M
    Pos limit;
38
#ifdef LONGEST_MATCH_SLOW
39
    Pos limit_base;
40
#else
41
222M
    int32_t early_exit;
42
222M
#endif
43
222M
    uint32_t chain_length, nice_match, best_len, offset;
44
222M
    uint32_t lookahead = s->lookahead;
45
222M
    Pos match_offset = 0;
46
222M
    uint64_t scan_start;
47
222M
    uint64_t scan_end;
48
49
222M
#define GOTO_NEXT_CHAIN \
50
222M
    if (--chain_length && (cur_match = prev[cur_match & wmask]) > limit) \
51
222M
        continue; \
52
222M
    return best_len;
53
54
    /* The code is optimized for STD_MAX_MATCH-2 multiple of 16. */
55
222M
    Assert(STD_MAX_MATCH == 258, "Code too clever");
56
57
222M
    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
222M
    offset = best_len-1;
63
222M
    if (best_len >= sizeof(uint32_t)) {
64
4.76M
        offset -= 2;
65
4.76M
        if (best_len >= sizeof(uint64_t))
66
1.95M
            offset -= 4;
67
4.76M
    }
68
69
222M
    scan_start = zng_memread_8(scan);
70
222M
    scan_end = zng_memread_8(scan+offset);
71
222M
    mbase_end  = (mbase_start+offset);
72
73
    /* Do not waste too much time if we already have a good match */
74
222M
    chain_length = s->max_chain_length;
75
222M
    if (best_len >= s->good_match)
76
1.94M
        chain_length >>= 2;
77
222M
    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
222M
    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
222M
    early_exit = s->level < EARLY_EXIT_TRIGGER_LEVEL;
117
222M
#endif
118
222M
    Assert((unsigned long)strstart <= s->window_size - MIN_LOOKAHEAD, "need lookahead");
119
258M
    for (;;) {
120
258M
        if (cur_match >= strstart)
121
44
            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
258M
        if (best_len < sizeof(uint32_t)) {
131
283M
            for (;;) {
132
283M
                if (zng_memcmp_2(mbase_end+cur_match, &scan_end) == 0 &&
133
283M
                    zng_memcmp_2(mbase_start+cur_match, &scan_start) == 0)
134
29.6M
                    break;
135
254M
                GOTO_NEXT_CHAIN;
136
254M
            }
137
217M
        } else if (best_len >= sizeof(uint64_t)) {
138
273M
            for (;;) {
139
273M
                if (zng_memcmp_8(mbase_end+cur_match, &scan_end) == 0 &&
140
273M
                    zng_memcmp_8(mbase_start+cur_match, &scan_start) == 0)
141
8.82M
                    break;
142
264M
                GOTO_NEXT_CHAIN;
143
264M
            }
144
23.5M
        } else {
145
365M
            for (;;) {
146
365M
                if (zng_memcmp_4(mbase_end+cur_match, &scan_end) == 0 &&
147
365M
                    zng_memcmp_4(mbase_start+cur_match, &scan_start) == 0)
148
8.57M
                    break;
149
357M
                GOTO_NEXT_CHAIN;
150
357M
            }
151
23.5M
        }
152
47.0M
        uint32_t len = COMPARE256(scan+2, mbase_start+cur_match+2) + 2;
153
47.0M
        Assert(scan+len <= window+(unsigned)(s->window_size-1), "wild scan");
154
155
47.0M
        if (len > best_len) {
156
46.0M
            uint32_t match_start = cur_match - match_offset;
157
46.0M
            s->match_start = match_start;
158
159
            /* Do not look for matches beyond the end of the input. */
160
46.0M
            if (len > lookahead)
161
3.14k
                return lookahead;
162
46.0M
            best_len = len;
163
46.0M
            if (best_len >= nice_match)
164
2.09M
                return best_len;
165
166
43.9M
            offset = best_len-1;
167
43.9M
            if (best_len >= sizeof(uint32_t)) {
168
43.9M
                offset -= 2;
169
43.9M
                if (best_len >= sizeof(uint64_t))
170
15.9M
                    offset -= 4;
171
43.9M
            }
172
173
43.9M
            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
43.9M
            mbase_end = (mbase_start+offset);
226
43.9M
        }
227
1.04M
#ifndef LONGEST_MATCH_SLOW
228
1.04M
        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
778
            break;
233
778
        }
234
44.9M
#endif
235
44.9M
        GOTO_NEXT_CHAIN;
236
44.9M
    }
237
822
    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
222M
}
longest_match_slow_avx2
Line
Count
Source
29
19.4M
Z_INTERNAL uint32_t LONGEST_MATCH(deflate_state *const s, Pos cur_match) {
30
19.4M
    unsigned int strstart = s->strstart;
31
19.4M
    const unsigned wmask = s->w_mask;
32
19.4M
    unsigned char *window = s->window;
33
19.4M
    unsigned char *scan = window + strstart;
34
19.4M
    Z_REGISTER unsigned char *mbase_start = window;
35
19.4M
    Z_REGISTER unsigned char *mbase_end;
36
19.4M
    const Pos *prev = s->prev;
37
19.4M
    Pos limit;
38
19.4M
#ifdef LONGEST_MATCH_SLOW
39
19.4M
    Pos limit_base;
40
#else
41
    int32_t early_exit;
42
#endif
43
19.4M
    uint32_t chain_length, nice_match, best_len, offset;
44
19.4M
    uint32_t lookahead = s->lookahead;
45
19.4M
    Pos match_offset = 0;
46
19.4M
    uint64_t scan_start;
47
19.4M
    uint64_t scan_end;
48
49
19.4M
#define GOTO_NEXT_CHAIN \
50
19.4M
    if (--chain_length && (cur_match = prev[cur_match & wmask]) > limit) \
51
19.4M
        continue; \
52
19.4M
    return best_len;
53
54
    /* The code is optimized for STD_MAX_MATCH-2 multiple of 16. */
55
19.4M
    Assert(STD_MAX_MATCH == 258, "Code too clever");
56
57
19.4M
    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
19.4M
    offset = best_len-1;
63
19.4M
    if (best_len >= sizeof(uint32_t)) {
64
2.34M
        offset -= 2;
65
2.34M
        if (best_len >= sizeof(uint64_t))
66
484k
            offset -= 4;
67
2.34M
    }
68
69
19.4M
    scan_start = zng_memread_8(scan);
70
19.4M
    scan_end = zng_memread_8(scan+offset);
71
19.4M
    mbase_end  = (mbase_start+offset);
72
73
    /* Do not waste too much time if we already have a good match */
74
19.4M
    chain_length = s->max_chain_length;
75
19.4M
    if (best_len >= s->good_match)
76
72.6k
        chain_length >>= 2;
77
19.4M
    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
19.4M
    limit = strstart > MAX_DIST(s) ? (Pos)(strstart - MAX_DIST(s)) : 0;
83
19.4M
#ifdef LONGEST_MATCH_SLOW
84
19.4M
    limit_base = limit;
85
19.4M
    if (best_len >= STD_MIN_MATCH) {
86
        /* We're continuing search (lazy evaluation). */
87
3.78M
        uint32_t i, hash;
88
3.78M
        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.78M
        hash = s->update_hash(0, scan[1]);
95
3.78M
        hash = s->update_hash(hash, scan[2]);
96
97
20.4M
        for (i = 3; i <= best_len; i++) {
98
16.6M
            hash = s->update_hash(hash, scan[i]);
99
100
            /* If we're starting with best_len >= 3, we can use offset search. */
101
16.6M
            pos = s->head[hash];
102
16.6M
            if (pos < cur_match) {
103
3.46M
                match_offset = (Pos)(i - 2);
104
3.46M
                cur_match = pos;
105
3.46M
            }
106
16.6M
        }
107
108
        /* Update offset-dependent variables */
109
3.78M
        limit = limit_base+match_offset;
110
3.78M
        if (cur_match <= limit)
111
902k
            goto break_matching;
112
2.88M
        mbase_start -= match_offset;
113
2.88M
        mbase_end -= match_offset;
114
2.88M
    }
115
#else
116
    early_exit = s->level < EARLY_EXIT_TRIGGER_LEVEL;
117
#endif
118
18.5M
    Assert((unsigned long)strstart <= s->window_size - MIN_LOOKAHEAD, "need lookahead");
119
26.4M
    for (;;) {
120
26.4M
        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
26.4M
        if (best_len < sizeof(uint32_t)) {
131
66.3M
            for (;;) {
132
66.3M
                if (zng_memcmp_2(mbase_end+cur_match, &scan_end) == 0 &&
133
66.3M
                    zng_memcmp_2(mbase_start+cur_match, &scan_start) == 0)
134
7.91M
                    break;
135
58.4M
                GOTO_NEXT_CHAIN;
136
58.4M
            }
137
20.0M
        } else if (best_len >= sizeof(uint64_t)) {
138
49.9M
            for (;;) {
139
49.9M
                if (zng_memcmp_8(mbase_end+cur_match, &scan_end) == 0 &&
140
49.9M
                    zng_memcmp_8(mbase_start+cur_match, &scan_start) == 0)
141
2.53M
                    break;
142
47.3M
                GOTO_NEXT_CHAIN;
143
47.3M
            }
144
3.26M
        } else {
145
40.2M
            for (;;) {
146
40.2M
                if (zng_memcmp_4(mbase_end+cur_match, &scan_end) == 0 &&
147
40.2M
                    zng_memcmp_4(mbase_start+cur_match, &scan_start) == 0)
148
607k
                    break;
149
39.6M
                GOTO_NEXT_CHAIN;
150
39.6M
            }
151
3.26M
        }
152
11.0M
        uint32_t len = COMPARE256(scan+2, mbase_start+cur_match+2) + 2;
153
11.0M
        Assert(scan+len <= window+(unsigned)(s->window_size-1), "wild scan");
154
155
11.0M
        if (len > best_len) {
156
9.71M
            uint32_t match_start = cur_match - match_offset;
157
9.71M
            s->match_start = match_start;
158
159
            /* Do not look for matches beyond the end of the input. */
160
9.71M
            if (len > lookahead)
161
5.83k
                return lookahead;
162
9.70M
            best_len = len;
163
9.70M
            if (best_len >= nice_match)
164
1.62M
                return best_len;
165
166
8.08M
            offset = best_len-1;
167
8.08M
            if (best_len >= sizeof(uint32_t)) {
168
4.37M
                offset -= 2;
169
4.37M
                if (best_len >= sizeof(uint64_t))
170
1.62M
                    offset -= 4;
171
4.37M
            }
172
173
8.08M
            scan_end = zng_memread_8(scan+offset);
174
175
8.08M
#ifdef LONGEST_MATCH_SLOW
176
            /* Look for a better string offset */
177
8.08M
            if (UNLIKELY(len > STD_MIN_MATCH && match_start + len < strstart)) {
178
4.25M
                Pos pos, next_pos;
179
4.25M
                uint32_t i, hash;
180
4.25M
                unsigned char *scan_endstr;
181
182
                /* Go back to offset 0 */
183
4.25M
                cur_match -= match_offset;
184
4.25M
                match_offset = 0;
185
4.25M
                next_pos = cur_match;
186
104M
                for (i = 0; i <= len - STD_MIN_MATCH; i++) {
187
101M
                    pos = prev[(cur_match + i) & wmask];
188
101M
                    if (pos < next_pos) {
189
                        /* Hash chain is more distant, use it */
190
5.65M
                        if (pos <= limit_base + i)
191
977k
                            goto break_matching;
192
4.67M
                        next_pos = pos;
193
4.67M
                        match_offset = (Pos)i;
194
4.67M
                    }
195
101M
                }
196
                /* Switch cur_match to next_pos chain */
197
3.27M
                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
3.27M
                scan_endstr = scan + len - (STD_MIN_MATCH+1);
205
206
3.27M
                hash = s->update_hash(0, scan_endstr[0]);
207
3.27M
                hash = s->update_hash(hash, scan_endstr[1]);
208
3.27M
                hash = s->update_hash(hash, scan_endstr[2]);
209
210
3.27M
                pos = s->head[hash];
211
3.27M
                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
3.27M
                limit = limit_base+match_offset;
220
3.27M
                mbase_start = window-match_offset;
221
3.27M
                mbase_end = (mbase_start+offset);
222
3.27M
                continue;
223
3.27M
            }
224
3.82M
#endif
225
3.82M
            mbase_end = (mbase_start+offset);
226
3.82M
        }
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
5.17M
        GOTO_NEXT_CHAIN;
236
5.17M
    }
237
0
    return best_len;
238
239
0
#ifdef LONGEST_MATCH_SLOW
240
1.88M
break_matching:
241
242
1.88M
    if (best_len < s->lookahead)
243
1.87M
        return best_len;
244
245
1.26k
    return s->lookahead;
246
1.88M
#endif
247
1.88M
}
Unexecuted instantiation: longest_match_avx512
Unexecuted instantiation: longest_match_slow_avx512
248
249
#undef LONGEST_MATCH_SLOW
250
#undef LONGEST_MATCH