Coverage Report

Created: 2025-08-26 06:45

/src/zlib-ng/insert_string_tpl.h
Line
Count
Source
1
#ifndef INSERT_STRING_H_
2
#define INSERT_STRING_H_
3
4
/* insert_string_tpl.h -- Private insert_string functions shared with more than
5
 *                        one insert string implementation
6
 *
7
 * Copyright (C) 1995-2024 Jean-loup Gailly and Mark Adler
8
 *
9
 * Copyright (C) 2013 Intel Corporation. All rights reserved.
10
 * Authors:
11
 *  Wajdi Feghali   <wajdi.k.feghali@intel.com>
12
 *  Jim Guilford    <james.guilford@intel.com>
13
 *  Vinodh Gopal    <vinodh.gopal@intel.com>
14
 *  Erdinc Ozturk   <erdinc.ozturk@intel.com>
15
 *  Jim Kukunas     <james.t.kukunas@linux.intel.com>
16
 *
17
 * Portions are Copyright (C) 2016 12Sided Technology, LLC.
18
 * Author:
19
 *  Phil Vachon     <pvachon@12sidedtech.com>
20
 *
21
 * For conditions of distribution and use, see copyright notice in zlib.h
22
 *
23
 */
24
25
#include "zmemory.h"
26
27
#ifndef HASH_CALC_OFFSET
28
3.08M
#  define HASH_CALC_OFFSET 0
29
#endif
30
#ifndef HASH_CALC_MASK
31
7.15M
#  define HASH_CALC_MASK HASH_MASK
32
#endif
33
#ifndef HASH_CALC_READ
34
#  if BYTE_ORDER == LITTLE_ENDIAN
35
#    define HASH_CALC_READ \
36
7.15M
        val = zng_memread_4(strstart);
37
#  else
38
#    define HASH_CALC_READ \
39
        val = ZSWAP32(zng_memread_4(strstart));
40
#  endif
41
#endif
42
43
/* ===========================================================================
44
 * Update a hash value with the given input byte
45
 * IN  assertion: all calls to UPDATE_HASH are made with consecutive
46
 *    input characters, so that a running hash key can be computed from the
47
 *    previous key instead of complete recalculation each time.
48
 */
49
4.72M
Z_INTERNAL uint32_t UPDATE_HASH(uint32_t h, uint32_t val) {
50
4.72M
    HASH_CALC(h, val);
51
4.72M
    return h & HASH_CALC_MASK;
52
4.72M
}
Unexecuted instantiation: update_hash
update_hash_roll
Line
Count
Source
49
4.72M
Z_INTERNAL uint32_t UPDATE_HASH(uint32_t h, uint32_t val) {
50
4.72M
    HASH_CALC(h, val);
51
4.72M
    return h & HASH_CALC_MASK;
52
4.72M
}
53
54
/* ===========================================================================
55
 * Quick insert string str in the dictionary and set match_head to the previous head
56
 * of the hash chain (the most recent string with same hash key). Return
57
 * the previous length of the hash chain.
58
 */
59
5.12M
Z_INTERNAL Pos QUICK_INSERT_STRING(deflate_state *const s, uint32_t str) {
60
5.12M
    uint8_t *strstart = s->window + str + HASH_CALC_OFFSET;
61
5.12M
    uint32_t val, hm;
62
5.12M
    Pos head;
63
64
5.12M
    HASH_CALC_VAR_INIT;
65
5.12M
    HASH_CALC_READ;
66
5.12M
    HASH_CALC(HASH_CALC_VAR, val);
67
5.12M
    HASH_CALC_VAR &= HASH_CALC_MASK;
68
5.12M
    hm = HASH_CALC_VAR;
69
70
5.12M
    head = s->head[hm];
71
5.12M
    if (LIKELY(head != str)) {
72
5.11M
        s->prev[str & s->w_mask] = head;
73
5.11M
        s->head[hm] = (Pos)str;
74
5.11M
    }
75
5.12M
    return head;
76
5.12M
}
quick_insert_string
Line
Count
Source
59
2.87M
Z_INTERNAL Pos QUICK_INSERT_STRING(deflate_state *const s, uint32_t str) {
60
2.87M
    uint8_t *strstart = s->window + str + HASH_CALC_OFFSET;
61
2.87M
    uint32_t val, hm;
62
2.87M
    Pos head;
63
64
2.87M
    HASH_CALC_VAR_INIT;
65
2.87M
    HASH_CALC_READ;
66
2.87M
    HASH_CALC(HASH_CALC_VAR, val);
67
2.87M
    HASH_CALC_VAR &= HASH_CALC_MASK;
68
2.87M
    hm = HASH_CALC_VAR;
69
70
2.87M
    head = s->head[hm];
71
2.87M
    if (LIKELY(head != str)) {
72
2.86M
        s->prev[str & s->w_mask] = head;
73
2.86M
        s->head[hm] = (Pos)str;
74
2.86M
    }
75
2.87M
    return head;
76
2.87M
}
quick_insert_string_roll
Line
Count
Source
59
2.25M
Z_INTERNAL Pos QUICK_INSERT_STRING(deflate_state *const s, uint32_t str) {
60
2.25M
    uint8_t *strstart = s->window + str + HASH_CALC_OFFSET;
61
2.25M
    uint32_t val, hm;
62
2.25M
    Pos head;
63
64
2.25M
    HASH_CALC_VAR_INIT;
65
2.25M
    HASH_CALC_READ;
66
2.25M
    HASH_CALC(HASH_CALC_VAR, val);
67
2.25M
    HASH_CALC_VAR &= HASH_CALC_MASK;
68
2.25M
    hm = HASH_CALC_VAR;
69
70
2.25M
    head = s->head[hm];
71
2.25M
    if (LIKELY(head != str)) {
72
2.25M
        s->prev[str & s->w_mask] = head;
73
2.25M
        s->head[hm] = (Pos)str;
74
2.25M
    }
75
2.25M
    return head;
76
2.25M
}
77
78
/* ===========================================================================
79
 * Insert string str in the dictionary and set match_head to the previous head
80
 * of the hash chain (the most recent string with same hash key). Return
81
 * the previous length of the hash chain.
82
 * IN  assertion: all calls to INSERT_STRING are made with consecutive
83
 *    input characters and the first STD_MIN_MATCH bytes of str are valid
84
 *    (except for the last STD_MIN_MATCH-1 bytes of the input file).
85
 */
86
471k
Z_INTERNAL void INSERT_STRING(deflate_state *const s, uint32_t str, uint32_t count) {
87
471k
    uint8_t *strstart = s->window + str + HASH_CALC_OFFSET;
88
471k
    uint8_t *strend = strstart + count;
89
90
    /* Local pointers to avoid indirection */
91
471k
    Pos *headp = s->head;
92
471k
    Pos *prevp = s->prev;
93
471k
    const unsigned int w_mask = s->w_mask;
94
95
9.11M
    for (Pos idx = (Pos)str; strstart < strend; idx++, strstart++) {
96
8.64M
        uint32_t val, hm;
97
98
8.64M
        HASH_CALC_VAR_INIT;
99
8.64M
        HASH_CALC_READ;
100
8.64M
        HASH_CALC(HASH_CALC_VAR, val);
101
8.64M
        HASH_CALC_VAR &= HASH_CALC_MASK;
102
8.64M
        hm = HASH_CALC_VAR;
103
104
8.64M
        Pos head = headp[hm];
105
8.64M
        if (LIKELY(head != idx)) {
106
8.63M
            prevp[idx & w_mask] = head;
107
8.63M
            headp[hm] = idx;
108
8.63M
        }
109
8.64M
    }
110
471k
}
insert_string
Line
Count
Source
86
212k
Z_INTERNAL void INSERT_STRING(deflate_state *const s, uint32_t str, uint32_t count) {
87
212k
    uint8_t *strstart = s->window + str + HASH_CALC_OFFSET;
88
212k
    uint8_t *strend = strstart + count;
89
90
    /* Local pointers to avoid indirection */
91
212k
    Pos *headp = s->head;
92
212k
    Pos *prevp = s->prev;
93
212k
    const unsigned int w_mask = s->w_mask;
94
95
4.49M
    for (Pos idx = (Pos)str; strstart < strend; idx++, strstart++) {
96
4.27M
        uint32_t val, hm;
97
98
4.27M
        HASH_CALC_VAR_INIT;
99
4.27M
        HASH_CALC_READ;
100
4.27M
        HASH_CALC(HASH_CALC_VAR, val);
101
4.27M
        HASH_CALC_VAR &= HASH_CALC_MASK;
102
4.27M
        hm = HASH_CALC_VAR;
103
104
4.27M
        Pos head = headp[hm];
105
4.27M
        if (LIKELY(head != idx)) {
106
4.27M
            prevp[idx & w_mask] = head;
107
4.27M
            headp[hm] = idx;
108
4.27M
        }
109
4.27M
    }
110
212k
}
insert_string_roll
Line
Count
Source
86
259k
Z_INTERNAL void INSERT_STRING(deflate_state *const s, uint32_t str, uint32_t count) {
87
259k
    uint8_t *strstart = s->window + str + HASH_CALC_OFFSET;
88
259k
    uint8_t *strend = strstart + count;
89
90
    /* Local pointers to avoid indirection */
91
259k
    Pos *headp = s->head;
92
259k
    Pos *prevp = s->prev;
93
259k
    const unsigned int w_mask = s->w_mask;
94
95
4.62M
    for (Pos idx = (Pos)str; strstart < strend; idx++, strstart++) {
96
4.36M
        uint32_t val, hm;
97
98
4.36M
        HASH_CALC_VAR_INIT;
99
4.36M
        HASH_CALC_READ;
100
4.36M
        HASH_CALC(HASH_CALC_VAR, val);
101
4.36M
        HASH_CALC_VAR &= HASH_CALC_MASK;
102
4.36M
        hm = HASH_CALC_VAR;
103
104
4.36M
        Pos head = headp[hm];
105
4.36M
        if (LIKELY(head != idx)) {
106
4.36M
            prevp[idx & w_mask] = head;
107
4.36M
            headp[hm] = idx;
108
4.36M
        }
109
4.36M
    }
110
259k
}
111
#endif