Coverage Report

Created: 2026-12-01 06:39

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/zlib-ng/adler32_p.h
Line
Count
Source
1
/* adler32_p.h -- Private inline functions and macros shared with
2
 *                different computation of the Adler-32 checksum
3
 *                of a data stream.
4
 * Copyright (C) 1995-2011, 2016 Mark Adler
5
 * For conditions of distribution and use, see copyright notice in zlib.h
6
 */
7
8
#ifndef ADLER32_P_H
9
#define ADLER32_P_H
10
11
#include "zendian.h"
12
13
131k
#define BASE 65521U     /* largest prime smaller than 65536 */
14
0
#define NMAX 5552
15
/* NMAX is the largest n such that 255n(n+1)/2 + (n+1)(BASE-1) <= 2^32-1 */
16
0
#define NMAX_ALIGNED8 (NMAX & ~7)
17
/* NMAX rounded down to a multiple of 8 is 5552 */
18
#define NMAX_ALIGNED32 (NMAX & ~31)
19
/* NMAX rounded down to a multiple of 32 is 5536 */
20
21
61.4k
#define ADLER_DO1(sum1, sum2, buf, i)  {(sum1) += buf[(i)]; (sum2) += (sum1);}
22
26.5k
#define ADLER_DO2(sum1, sum2, buf, i)  {ADLER_DO1(sum1, sum2, buf, i); ADLER_DO1(sum1, sum2, buf, i+1);}
23
8.79k
#define ADLER_DO4(sum1, sum2, buf, i)  {ADLER_DO2(sum1, sum2, buf, i); ADLER_DO2(sum1, sum2, buf, i+2);}
24
#define ADLER_DO8(sum1, sum2, buf, i)  {ADLER_DO4(sum1, sum2, buf, i); ADLER_DO4(sum1, sum2, buf, i+4);}
25
#define ADLER_DO16(sum1, sum2, buf)    {ADLER_DO8(sum1, sum2, buf, 0); ADLER_DO8(sum1, sum2, buf, 8);}
26
27
Z_FORCEINLINE static void adler32_copy_align(uint32_t *Z_RESTRICT adler, uint8_t *dst, const uint8_t *buf, size_t len,
28
0
                                             uint32_t *Z_RESTRICT sum2, const int MAX_LEN, const int COPY) {
29
0
    Z_UNUSED(MAX_LEN);
30
0
    if (len & 1) {
31
0
        if (COPY) {
32
0
            *dst = *buf;
33
0
            dst += 1;
34
0
        }
35
0
        ADLER_DO1(*adler, *sum2, buf, 0);
36
0
        buf += 1;
37
0
    }
38
0
    if (len & 2) {
39
0
        if (COPY) {
40
0
            memcpy(dst, buf, 2);
41
0
            dst += 2;
42
0
        }
43
0
        ADLER_DO2(*adler, *sum2, buf, 0);
44
0
        buf += 2;
45
0
    }
46
0
    while (len >= 4) {
47
0
        if (COPY) {
48
0
            memcpy(dst, buf, 4);
49
0
            dst += 4;
50
0
        }
51
0
        len -= 4;
52
0
        ADLER_DO4(*adler, *sum2, buf, 0);
53
0
        buf += 4;
54
0
    }
55
0
}
Unexecuted instantiation: adler32_ssse3.c:adler32_copy_align
Unexecuted instantiation: adler32_sse42.c:adler32_copy_align
Unexecuted instantiation: adler32_avx2.c:adler32_copy_align
Unexecuted instantiation: adler32_avx2_vnni.c:adler32_copy_align
Unexecuted instantiation: adler32_avx512.c:adler32_copy_align
Unexecuted instantiation: adler32_avx512_vnni.c:adler32_copy_align
Unexecuted instantiation: adler32_c.c:adler32_copy_align
56
57
/* SIMD Within A Register (SWAR) scalar adler32. Splits bytes into
58
 * even/odd lanes packed as 4x16-bit in uint64_t, with prefix sums for s2.
59
 * Reduction uses multiply-and-shift with positional weight constants.
60
 *
61
 * Technique pioneered by Michael Niedermayer <michaelni@gmx.at>.
62
 * Max chunk: 23 iterations * 8 bytes = 184 (255*23 = 5865 < 65535). */
63
#define ADLER32_SWAR_MAX_BYTES   (23 * 8)
64
0
#define ADLER32_SWAR_EVEN_MASK   0x00FF00FF00FF00FFULL
65
0
#define ADLER32_SWAR_HSUM        0x1000100010001ULL
66
67
Z_FORCEINLINE static void adler32_swar(uint32_t *adler, uint8_t *dst, const uint8_t *buf, size_t len,
68
0
                                       uint32_t *sum2, const int COPY) {
69
0
    uint64_t sum_even = 0, sum_odd = 0, prefix_even = 0, prefix_odd = 0;
70
71
0
    *sum2 += *adler * (uint32_t)len;
72
73
0
    const uint64_t *src64 = (const uint64_t *)buf;
74
75
0
    while (len >= 16) {
76
0
        uint64_t v0 = src64[0];
77
0
        uint64_t v1 = src64[1];
78
0
        if (COPY) {
79
0
            memcpy(dst, &v0, sizeof(v0));
80
0
            memcpy(dst + 8, &v1, sizeof(v1));
81
0
            dst += 16;
82
0
        }
83
84
0
        prefix_even += sum_even;
85
0
        prefix_odd += sum_odd;
86
0
        sum_even +=  v0       & ADLER32_SWAR_EVEN_MASK;
87
0
        sum_odd  += (v0 >> 8) & ADLER32_SWAR_EVEN_MASK;
88
89
0
        prefix_even += sum_even;
90
0
        prefix_odd += sum_odd;
91
0
        sum_even +=  v1       & ADLER32_SWAR_EVEN_MASK;
92
0
        sum_odd  += (v1 >> 8) & ADLER32_SWAR_EVEN_MASK;
93
94
0
        src64 += 2;
95
0
        len -= 16;
96
0
    }
97
98
    /* Handle remaining 8 bytes if present */
99
0
    if (len >= 8) {
100
0
        uint64_t v = *src64;
101
0
        if (COPY)
102
0
            memcpy(dst, &v, sizeof(v));
103
104
0
        prefix_even += sum_even;
105
0
        prefix_odd += sum_odd;
106
0
        sum_even +=  v       & ADLER32_SWAR_EVEN_MASK;
107
0
        sum_odd  += (v >> 8) & ADLER32_SWAR_EVEN_MASK;
108
0
    }
109
110
    /* Horizontal sum of 4x16-bit lanes for s1 */
111
0
    *adler += (uint32_t)(((sum_even + sum_odd) * ADLER32_SWAR_HSUM) >> 48);
112
113
    /* Widen prefix sums to 32-bit pairs and horizontal sum for s2 */
114
0
    uint64_t pe_lo = prefix_even & 0xFFFF0000FFFFULL;
115
0
    uint64_t pe_hi = (prefix_even >> 16) & 0xFFFF0000FFFFULL;
116
0
    uint64_t po_lo = prefix_odd & 0xFFFF0000FFFFULL;
117
0
    uint64_t po_hi = (prefix_odd >> 16) & 0xFFFF0000FFFFULL;
118
119
0
    *sum2 += (uint32_t)(((pe_lo + po_lo + pe_hi + po_hi) * 0x800000008ULL) >> 32);
120
121
    /* Positional weights [8,7,6,5,4,3,2,1] per 8-byte group for s2.
122
     * On big-endian the even mask captures odd-index memory bytes (b1,b3,b5,b7)
123
     * so HSUM (+1 per odd-index byte) must be applied to sum_even, not sum_odd. */
124
0
#if BYTE_ORDER == LITTLE_ENDIAN
125
0
    *sum2 += 2 * (uint32_t)((sum_even * 0x4000300020001ULL) >> 48)
126
0
           +     (uint32_t)((sum_odd  * ADLER32_SWAR_HSUM) >> 48)
127
0
           + 2 * (uint32_t)((sum_odd  * 0x3000200010000ULL) >> 48);
128
#else
129
    *sum2 += 2 * (uint32_t)((sum_even * 0x0000100020003ULL) >> 48)
130
           +     (uint32_t)((sum_even * ADLER32_SWAR_HSUM) >> 48)
131
           + 2 * (uint32_t)((sum_odd  * 0x1000200030004ULL) >> 48);
132
#endif
133
0
}
Unexecuted instantiation: adler32_ssse3.c:adler32_swar
Unexecuted instantiation: adler32_sse42.c:adler32_swar
Unexecuted instantiation: adler32_avx2.c:adler32_swar
Unexecuted instantiation: adler32_avx2_vnni.c:adler32_swar
Unexecuted instantiation: adler32_avx512.c:adler32_swar
Unexecuted instantiation: adler32_avx512_vnni.c:adler32_swar
Unexecuted instantiation: adler32_c.c:adler32_swar
134
135
Z_FORCEINLINE static uint32_t adler32_copy_tail(uint32_t adler, uint8_t *dst, const uint8_t *buf, size_t len,
136
11.1k
                                                uint32_t sum2, const int REBASE, const int MAX_LEN, const int COPY) {
137
11.1k
    if (len) {
138
11.1k
        Z_UNUSED(MAX_LEN);
139
        /* Process using packed 64-bit arithmetic when source is aligned */
140
11.1k
        while (len >= 8 && ((uintptr_t)buf & 7) == 0) {
141
0
            size_t chunk = MIN(ALIGN_DOWN(len, (size_t)8), (size_t)ADLER32_SWAR_MAX_BYTES);
142
0
            adler32_swar(&adler, dst, buf, chunk, &sum2, COPY);
143
0
            buf += chunk;
144
0
            if (COPY)
145
0
                dst += chunk;
146
0
            len -= chunk;
147
0
        }
148
        /* DO4 loop avoids GCC x86 register pressure from hoisted DO8/DO16 loads. */
149
19.9k
        while (len >= 4) {
150
8.79k
            if (COPY) {
151
8.79k
                memcpy(dst, buf, 4);
152
8.79k
                dst += 4;
153
8.79k
            }
154
8.79k
            len -= 4;
155
8.79k
            ADLER_DO4(adler, sum2, buf, 0);
156
8.79k
            buf += 4;
157
8.79k
        }
158
11.1k
        if (len & 2) {
159
8.93k
            if (COPY) {
160
8.93k
                memcpy(dst, buf, 2);
161
8.93k
                dst += 2;
162
8.93k
            }
163
8.93k
            ADLER_DO2(adler, sum2, buf, 0);
164
8.93k
            buf += 2;
165
8.93k
        }
166
11.1k
        if (len & 1) {
167
8.37k
            if (COPY)
168
8.37k
                *dst = *buf;
169
8.37k
            ADLER_DO1(adler, sum2, buf, 0);
170
8.37k
        }
171
11.1k
    }
172
11.1k
    if (REBASE) {
173
11.1k
        adler %= BASE;
174
11.1k
        sum2 %= BASE;
175
11.1k
    }
176
    /* D = B * 65536 + A, see: https://en.wikipedia.org/wiki/Adler-32. */
177
11.1k
    return adler | (sum2 << 16);
178
11.1k
}
Unexecuted instantiation: adler32_ssse3.c:adler32_copy_tail
adler32_sse42.c:adler32_copy_tail
Line
Count
Source
136
2.38k
                                                uint32_t sum2, const int REBASE, const int MAX_LEN, const int COPY) {
137
2.38k
    if (len) {
138
2.38k
        Z_UNUSED(MAX_LEN);
139
        /* Process using packed 64-bit arithmetic when source is aligned */
140
2.38k
        while (len >= 8 && ((uintptr_t)buf & 7) == 0) {
141
0
            size_t chunk = MIN(ALIGN_DOWN(len, (size_t)8), (size_t)ADLER32_SWAR_MAX_BYTES);
142
0
            adler32_swar(&adler, dst, buf, chunk, &sum2, COPY);
143
0
            buf += chunk;
144
0
            if (COPY)
145
0
                dst += chunk;
146
0
            len -= chunk;
147
0
        }
148
        /* DO4 loop avoids GCC x86 register pressure from hoisted DO8/DO16 loads. */
149
5.93k
        while (len >= 4) {
150
3.54k
            if (COPY) {
151
3.54k
                memcpy(dst, buf, 4);
152
3.54k
                dst += 4;
153
3.54k
            }
154
3.54k
            len -= 4;
155
3.54k
            ADLER_DO4(adler, sum2, buf, 0);
156
3.54k
            buf += 4;
157
3.54k
        }
158
2.38k
        if (len & 2) {
159
1.33k
            if (COPY) {
160
1.33k
                memcpy(dst, buf, 2);
161
1.33k
                dst += 2;
162
1.33k
            }
163
1.33k
            ADLER_DO2(adler, sum2, buf, 0);
164
1.33k
            buf += 2;
165
1.33k
        }
166
2.38k
        if (len & 1) {
167
1.24k
            if (COPY)
168
1.24k
                *dst = *buf;
169
1.24k
            ADLER_DO1(adler, sum2, buf, 0);
170
1.24k
        }
171
2.38k
    }
172
2.38k
    if (REBASE) {
173
2.38k
        adler %= BASE;
174
2.38k
        sum2 %= BASE;
175
2.38k
    }
176
    /* D = B * 65536 + A, see: https://en.wikipedia.org/wiki/Adler-32. */
177
2.38k
    return adler | (sum2 << 16);
178
2.38k
}
adler32_avx2.c:adler32_copy_tail
Line
Count
Source
136
8.77k
                                                uint32_t sum2, const int REBASE, const int MAX_LEN, const int COPY) {
137
8.77k
    if (len) {
138
8.77k
        Z_UNUSED(MAX_LEN);
139
        /* Process using packed 64-bit arithmetic when source is aligned */
140
8.77k
        while (len >= 8 && ((uintptr_t)buf & 7) == 0) {
141
0
            size_t chunk = MIN(ALIGN_DOWN(len, (size_t)8), (size_t)ADLER32_SWAR_MAX_BYTES);
142
0
            adler32_swar(&adler, dst, buf, chunk, &sum2, COPY);
143
0
            buf += chunk;
144
0
            if (COPY)
145
0
                dst += chunk;
146
0
            len -= chunk;
147
0
        }
148
        /* DO4 loop avoids GCC x86 register pressure from hoisted DO8/DO16 loads. */
149
14.0k
        while (len >= 4) {
150
5.24k
            if (COPY) {
151
5.24k
                memcpy(dst, buf, 4);
152
5.24k
                dst += 4;
153
5.24k
            }
154
5.24k
            len -= 4;
155
5.24k
            ADLER_DO4(adler, sum2, buf, 0);
156
5.24k
            buf += 4;
157
5.24k
        }
158
8.77k
        if (len & 2) {
159
7.59k
            if (COPY) {
160
7.59k
                memcpy(dst, buf, 2);
161
7.59k
                dst += 2;
162
7.59k
            }
163
7.59k
            ADLER_DO2(adler, sum2, buf, 0);
164
7.59k
            buf += 2;
165
7.59k
        }
166
8.77k
        if (len & 1) {
167
7.13k
            if (COPY)
168
7.13k
                *dst = *buf;
169
7.13k
            ADLER_DO1(adler, sum2, buf, 0);
170
7.13k
        }
171
8.77k
    }
172
8.77k
    if (REBASE) {
173
8.77k
        adler %= BASE;
174
8.77k
        sum2 %= BASE;
175
8.77k
    }
176
    /* D = B * 65536 + A, see: https://en.wikipedia.org/wiki/Adler-32. */
177
8.77k
    return adler | (sum2 << 16);
178
8.77k
}
Unexecuted instantiation: adler32_avx2_vnni.c:adler32_copy_tail
Unexecuted instantiation: adler32_avx512.c:adler32_copy_tail
Unexecuted instantiation: adler32_avx512_vnni.c:adler32_copy_tail
Unexecuted instantiation: adler32_c.c:adler32_copy_tail
179
180
#endif /* ADLER32_P_H */