Coverage Report

Created: 2026-07-16 06:59

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/zlib-ng/arch/generic/crc32_braid_c.c
Line
Count
Source
1
/* crc32_braid.c -- compute the CRC-32 of a data stream
2
 * Copyright (C) 1995-2022 Mark Adler
3
 * For conditions of distribution and use, see copyright notice in zlib.h
4
 *
5
 * This interleaved implementation of a CRC makes use of pipelined multiple
6
 * arithmetic-logic units, commonly found in modern CPU cores. It is due to
7
 * Kadatch and Jenkins (2010). See doc/crc-doc.1.0.pdf in this distribution.
8
 */
9
10
#include "zbuild.h"
11
#include "arch_functions.h"
12
13
/* Used by chorba fallback and by arch-specific implementations (s390 vx, risc-v zbc). */
14
#ifdef CRC32_BRAID_FALLBACK
15
16
#include "crc32_braid_tbl.h"
17
#include "crc32_p.h"
18
19
/*
20
  A CRC of a message is computed on BRAID_N braids of words in the message, where
21
  each word consists of BRAID_W bytes (4 or 8). If BRAID_N is 3, for example, then
22
  three running sparse CRCs are calculated respectively on each braid, at these
23
  indices in the array of words: 0, 3, 6, ..., 1, 4, 7, ..., and 2, 5, 8, ...
24
  This is done starting at a word boundary, and continues until as many blocks of
25
  BRAID_N * BRAID_W bytes as are available have been processed. The results are
26
  combined into a single CRC at the end. For this code, BRAID_N must be in the
27
  range 1..6 and BRAID_W must be 4 or 8. The upper limit on BRAID_N can be increased
28
  if desired by adding more #if blocks, extending the patterns apparent in the code.
29
  In addition, crc32 tables would need to be regenerated, if the maximum BRAID_N
30
  value is increased.
31
32
  BRAID_N and BRAID_W are chosen empirically by benchmarking the execution time
33
  on a given processor. The choices for BRAID_N and BRAID_W below were based on
34
  testing on Intel Kaby Lake i7, AMD Ryzen 7, ARM Cortex-A57, Sparc64-VII, PowerPC
35
  POWER9, and MIPS64 Octeon II processors.
36
  The Intel, AMD, and ARM processors were all fastest with BRAID_N=5, BRAID_W=8.
37
  The Sparc, PowerPC, and MIPS64 were all fastest at BRAID_N=5, BRAID_W=4.
38
  They were all tested with either gcc or clang, all using the -O3 optimization
39
  level. Your mileage may vary.
40
*/
41
42
/* ========================================================================= */
43
#ifdef BRAID_W
44
/*
45
  Return the CRC of the BRAID_W bytes in the word_t data, taking the
46
  least-significant byte of the word as the first byte of data, without any pre
47
  or post conditioning. This is used to combine the CRCs of each braid.
48
 */
49
#  if BYTE_ORDER == LITTLE_ENDIAN
50
0
static uint32_t crc_word(z_word_t data) {
51
0
    int k;
52
0
    for (k = 0; k < BRAID_W; k++)
53
0
        data = (data >> 8) ^ crc_table[data & 0xff];
54
0
    return (uint32_t)data;
55
0
}
56
#  elif BYTE_ORDER == BIG_ENDIAN
57
static z_word_t crc_word(z_word_t data) {
58
    int k;
59
    for (k = 0; k < BRAID_W; k++)
60
        data = (data << 8) ^
61
            crc_big_table[(data >> ((BRAID_W - 1) << 3)) & 0xff];
62
    return data;
63
}
64
#  endif /* BYTE_ORDER */
65
#endif /* BRAID_W */
66
67
/* ========================================================================= */
68
0
Z_INTERNAL uint32_t crc32_braid(uint32_t crc, const uint8_t *buf, size_t len) {
69
0
    crc = ~crc;
70
71
0
#ifdef BRAID_W
72
    /* If provided enough bytes, do a braided CRC calculation. */
73
0
    if (len >= BRAID_N * BRAID_W + BRAID_W - 1) {
74
0
        size_t blks;
75
0
        z_word_t const *words;
76
0
        int k;
77
78
        /* Compute the CRC up to a z_word_t boundary. */
79
0
        size_t align_diff = (size_t)MIN(ALIGN_DIFF(buf, BRAID_W), len);
80
0
        if (align_diff) {
81
0
            crc = crc32_copy_small(crc, NULL, buf, align_diff, BRAID_W - 1, 0);
82
0
            len -= align_diff;
83
0
            buf += align_diff;
84
0
        }
85
86
        /* Compute the CRC on as many BRAID_N z_word_t blocks as are available. */
87
0
        blks = len / (BRAID_N * BRAID_W);
88
0
        len -= blks * BRAID_N * BRAID_W;
89
0
        words = (z_word_t const *)buf;
90
91
0
        z_word_t crc0, word0, comb;
92
0
#if BRAID_N > 1
93
0
        z_word_t crc1, word1;
94
0
#if BRAID_N > 2
95
0
        z_word_t crc2, word2;
96
0
#if BRAID_N > 3
97
0
        z_word_t crc3, word3;
98
0
#if BRAID_N > 4
99
0
        z_word_t crc4, word4;
100
#if BRAID_N > 5
101
        z_word_t crc5, word5;
102
#endif
103
0
#endif
104
0
#endif
105
0
#endif
106
0
#endif
107
        /* Initialize the CRC for each braid. */
108
0
        crc0 = Z_WORD_FROM_LE(crc);
109
0
#if BRAID_N > 1
110
0
        crc1 = 0;
111
0
#if BRAID_N > 2
112
0
        crc2 = 0;
113
0
#if BRAID_N > 3
114
0
        crc3 = 0;
115
0
#if BRAID_N > 4
116
0
        crc4 = 0;
117
#if BRAID_N > 5
118
        crc5 = 0;
119
#endif
120
0
#endif
121
0
#endif
122
0
#endif
123
0
#endif
124
        /* Process the first blks-1 blocks, computing the CRCs on each braid independently. */
125
0
        while (--blks) {
126
            /* Load the word for each braid into registers. */
127
0
            word0 = crc0 ^ words[0];
128
0
#if BRAID_N > 1
129
0
            word1 = crc1 ^ words[1];
130
0
#if BRAID_N > 2
131
0
            word2 = crc2 ^ words[2];
132
0
#if BRAID_N > 3
133
0
            word3 = crc3 ^ words[3];
134
0
#if BRAID_N > 4
135
0
            word4 = crc4 ^ words[4];
136
#if BRAID_N > 5
137
            word5 = crc5 ^ words[5];
138
#endif
139
0
#endif
140
0
#endif
141
0
#endif
142
0
#endif
143
0
            words += BRAID_N;
144
145
            /* Compute and update the CRC for each word. The loop should get unrolled. */
146
0
            crc0 = BRAID_TABLE[0][word0 & 0xff];
147
0
#if BRAID_N > 1
148
0
            crc1 = BRAID_TABLE[0][word1 & 0xff];
149
0
#if BRAID_N > 2
150
0
            crc2 = BRAID_TABLE[0][word2 & 0xff];
151
0
#if BRAID_N > 3
152
0
            crc3 = BRAID_TABLE[0][word3 & 0xff];
153
0
#if BRAID_N > 4
154
0
            crc4 = BRAID_TABLE[0][word4 & 0xff];
155
#if BRAID_N > 5
156
            crc5 = BRAID_TABLE[0][word5 & 0xff];
157
#endif
158
0
#endif
159
0
#endif
160
0
#endif
161
0
#endif
162
0
            for (k = 1; k < BRAID_W; k++) {
163
0
                crc0 ^= BRAID_TABLE[k][(word0 >> (k << 3)) & 0xff];
164
0
#if BRAID_N > 1
165
0
                crc1 ^= BRAID_TABLE[k][(word1 >> (k << 3)) & 0xff];
166
0
#if BRAID_N > 2
167
0
                crc2 ^= BRAID_TABLE[k][(word2 >> (k << 3)) & 0xff];
168
0
#if BRAID_N > 3
169
0
                crc3 ^= BRAID_TABLE[k][(word3 >> (k << 3)) & 0xff];
170
0
#if BRAID_N > 4
171
0
                crc4 ^= BRAID_TABLE[k][(word4 >> (k << 3)) & 0xff];
172
#if BRAID_N > 5
173
                crc5 ^= BRAID_TABLE[k][(word5 >> (k << 3)) & 0xff];
174
#endif
175
0
#endif
176
0
#endif
177
0
#endif
178
0
#endif
179
0
            }
180
0
        }
181
182
        /* Process the last block, combining the CRCs of the BRAID_N braids at the same time. */
183
0
        comb = crc_word(crc0 ^ words[0]);
184
0
#if BRAID_N > 1
185
0
        comb = crc_word(crc1 ^ words[1] ^ comb);
186
0
#if BRAID_N > 2
187
0
        comb = crc_word(crc2 ^ words[2] ^ comb);
188
0
#if BRAID_N > 3
189
0
        comb = crc_word(crc3 ^ words[3] ^ comb);
190
0
#if BRAID_N > 4
191
0
        comb = crc_word(crc4 ^ words[4] ^ comb);
192
#if BRAID_N > 5
193
        comb = crc_word(crc5 ^ words[5] ^ comb);
194
#endif
195
0
#endif
196
0
#endif
197
0
#endif
198
0
#endif
199
0
        words += BRAID_N;
200
0
        Assert(comb <= UINT32_MAX, "comb should fit in uint32_t");
201
0
        crc = (uint32_t)Z_WORD_FROM_LE(comb);
202
203
        /* Update the pointer to the remaining bytes to process. */
204
0
        buf = (const unsigned char *)words;
205
0
    }
206
207
0
#endif /* BRAID_W */
208
209
    /* Complete the computation of the CRC on any remaining bytes. */
210
0
    return ~crc32_copy_small(crc, NULL, buf, len, (BRAID_N * BRAID_W) - 1, 0);
211
0
}
212
213
0
Z_INTERNAL uint32_t crc32_copy_braid(uint32_t crc, uint8_t *dst, const uint8_t *src, size_t len) {
214
0
    crc = crc32_braid(crc, src, len);
215
0
    memcpy(dst, src, len);
216
0
    return crc;
217
0
}
218
219
#endif /* CRC32_BRAID_FALLBACK */