diff options
author | Mark Adler <madler@alumni.caltech.edu> | 2018-12-11 01:11:38 -0800 |
---|---|---|
committer | Mark Adler <madler@alumni.caltech.edu> | 2018-12-26 12:26:52 -0800 |
commit | f8719f5ae5acdc31d3794ddfea8ac963359de41e (patch) | |
tree | 70327ff8a4953abd605665ecd458a3c4b1a66443 /crc32.c | |
parent | 41d86c73b21191a3fa9ea5f476fc9f1fc5e4f8b3 (diff) | |
download | zlib-f8719f5ae5acdc31d3794ddfea8ac963359de41e.tar.gz zlib-f8719f5ae5acdc31d3794ddfea8ac963359de41e.tar.bz2 zlib-f8719f5ae5acdc31d3794ddfea8ac963359de41e.zip |
Speed up software CRC-32 computation by a factor of 1.5 to 3.
Use the interleaved method of Kadatch and Jenkins in order to make
use of pipelined instructions through multiple ALUs in a single
core. This also speeds up and simplifies the combination of CRCs,
and updates the functions to pre-calculate and use an operator for
CRC combination.
Diffstat (limited to 'crc32.c')
-rw-r--r-- | crc32.c | 1070 |
1 files changed, 733 insertions, 337 deletions
@@ -2,11 +2,9 @@ | |||
2 | * Copyright (C) 1995-2006, 2010, 2011, 2012, 2016, 2018 Mark Adler | 2 | * Copyright (C) 1995-2006, 2010, 2011, 2012, 2016, 2018 Mark Adler |
3 | * For conditions of distribution and use, see copyright notice in zlib.h | 3 | * For conditions of distribution and use, see copyright notice in zlib.h |
4 | * | 4 | * |
5 | * Thanks to Rodney Brown <rbrown64@csc.com.au> for his contribution of faster | 5 | * This interleaved implementation of a CRC makes use of pipelined multiple |
6 | * CRC methods: exclusive-oring 32 bits of data at a time, and pre-computing | 6 | * arithmetic-logic units, commonly found in modern CPU cores. It is due to |
7 | * tables for updating the shift register in one step with three exclusive-ors | 7 | * Kadatch and Jenkins (2010). See doc/crc-doc.1.0.pdf in this distribution. |
8 | * instead of four steps with four exclusive-ors. This results in about a | ||
9 | * factor of two increase in speed on a Power PC G4 (PPC7455) using gcc -O3. | ||
10 | */ | 8 | */ |
11 | 9 | ||
12 | /* @(#) $Id$ */ | 10 | /* @(#) $Id$ */ |
@@ -14,13 +12,12 @@ | |||
14 | /* | 12 | /* |
15 | Note on the use of DYNAMIC_CRC_TABLE: there is no mutex or semaphore | 13 | Note on the use of DYNAMIC_CRC_TABLE: there is no mutex or semaphore |
16 | protection on the static variables used to control the first-use generation | 14 | protection on the static variables used to control the first-use generation |
17 | of the crc tables. Therefore, if you #define DYNAMIC_CRC_TABLE, you should | 15 | of the crc tables. Therefore, if you #define DYNAMIC_CRC_TABLE, you should |
18 | first call get_crc_table() to initialize the tables before allowing more than | 16 | first call get_crc_table() to initialize the tables before allowing more than |
19 | one thread to use crc32(). | 17 | one thread to use crc32(). |
20 | 18 | ||
21 | DYNAMIC_CRC_TABLE and MAKECRCH can be #defined to write out crc32.h. A main() | 19 | MAKECRCH can be #defined to write out crc32.h. A main() routine is also |
22 | routine is also produced, so that this one source file can be compiled to an | 20 | produced, so that this one source file can be compiled to an executable. |
23 | executable. | ||
24 | */ | 21 | */ |
25 | 22 | ||
26 | #ifdef MAKECRCH | 23 | #ifdef MAKECRCH |
@@ -30,161 +27,164 @@ | |||
30 | # endif /* !DYNAMIC_CRC_TABLE */ | 27 | # endif /* !DYNAMIC_CRC_TABLE */ |
31 | #endif /* MAKECRCH */ | 28 | #endif /* MAKECRCH */ |
32 | 29 | ||
33 | #include "zutil.h" /* for STDC and FAR definitions */ | 30 | #include "zutil.h" /* for Z_U4, Z_U8, z_crc_t, and FAR definitions */ |
31 | |||
32 | /* | ||
33 | A CRC of a message is computed on N braids of words in the message, where | ||
34 | each word consists of W bytes (4 or 8). If N is 3, for example, then three | ||
35 | running sparse CRCs are calculated respectively on each braid, at these | ||
36 | indices in the array of words: 0, 3, 6, ..., 1, 4, 7, ..., and 2, 5, 8, ... | ||
37 | This is done starting at a word boundary, and continues until as many blocks | ||
38 | of N * W bytes as are available have been processed. The results are combined | ||
39 | into a single CRC at the end. For this code, N must be in the range 1..6 and | ||
40 | W must be 4 or 8. The upper limit on N can be increased if desired by adding | ||
41 | more #if blocks, extending the patterns apparent in the code. In addition, | ||
42 | crc32.h would need to be regenerated, if the maximum N value is increased. | ||
43 | |||
44 | N and W are chosen empirically by benchmarking the execution time on a given | ||
45 | processor. The choices for N and W below were based on testing on Intel Kaby | ||
46 | Lake i7, AMD Ryzen 7, ARM Cortex-A57, Sparc64-VII, PowerPC POWER9, and MIPS64 | ||
47 | Octeon II processors. The Intel, AMD, and ARM processors were all fastest | ||
48 | with N=5, W=8. The Sparc, PowerPC, and MIPS64 were all fastest at N=5, W=4. | ||
49 | They were all tested with either gcc or clang, all using the -O3 optimization | ||
50 | level. Your mileage may vary. | ||
51 | */ | ||
34 | 52 | ||
35 | /* Definitions for doing the crc four data bytes at a time. */ | 53 | /* Define N */ |
36 | #if !defined(NOBYFOUR) && defined(Z_U4) | 54 | #ifdef Z_TESTN |
37 | # define BYFOUR | 55 | # define N Z_TESTN |
38 | #endif | ||
39 | #ifdef BYFOUR | ||
40 | local unsigned long crc32_little OF((unsigned long, | ||
41 | const unsigned char FAR *, z_size_t)); | ||
42 | local unsigned long crc32_big OF((unsigned long, | ||
43 | const unsigned char FAR *, z_size_t)); | ||
44 | # define TBLS 8 | ||
45 | #else | 56 | #else |
46 | # define TBLS 1 | 57 | # define N 5 |
47 | #endif /* BYFOUR */ | 58 | #endif |
59 | #if N < 1 || N > 6 | ||
60 | # error N must be in 1..6 | ||
61 | #endif | ||
48 | 62 | ||
49 | /* Local functions for crc concatenation */ | 63 | /* |
50 | #define GF2_DIM 32 /* dimension of GF(2) vectors (length of CRC) */ | 64 | z_crc_t must be at least 32 bits. z_word_t must be at least as long as |
51 | local z_crc_t gf2_matrix_times OF((const z_crc_t *mat, z_crc_t vec)); | 65 | z_crc_t. It is assumed here that z_word_t is either 32 bits or 64 bits, and |
52 | local uLong crc32_combine_ OF((uLong crc1, uLong crc2, z_off64_t len2)); | 66 | that bytes are eight bits. |
53 | local void crc32_combine_gen_ OF((z_crc_t *op, z_off64_t len2)); | 67 | */ |
54 | 68 | ||
55 | /* ========================================================================= */ | 69 | /* |
56 | local z_crc_t gf2_matrix_times(mat, vec) | 70 | Define W and the associated z_word_t type. If W is not defined, then a |
57 | const z_crc_t *mat; | 71 | braided calculation is not used, and the associated tables and code are not |
58 | z_crc_t vec; | 72 | compiled. |
59 | { | 73 | */ |
60 | z_crc_t sum; | 74 | #ifdef Z_TESTW |
61 | 75 | # if Z_TESTW-1 != -1 | |
62 | sum = 0; | 76 | # define W Z_TESTW |
63 | while (vec) { | 77 | # endif |
64 | if (vec & 1) | 78 | #else |
65 | sum ^= *mat; | 79 | # ifdef MAKECRCH |
66 | vec >>= 1; | 80 | # define W 8 /* required for MAKECRCH */ |
67 | mat++; | 81 | # else |
68 | } | 82 | # if defined(__x86_64__) || defined(__aarch64__) |
69 | return sum; | 83 | # define W 8 |
70 | } | 84 | # else |
85 | # define W 4 | ||
86 | # endif | ||
87 | # endif | ||
88 | #endif | ||
89 | #ifdef W | ||
90 | # if W == 8 && defined(Z_U8) | ||
91 | typedef Z_U8 z_word_t; | ||
92 | # elif defined(Z_U4) | ||
93 | # undef W | ||
94 | # define W 4 | ||
95 | typedef Z_U4 z_word_t; | ||
96 | # else | ||
97 | # undef W | ||
98 | # endif | ||
99 | #endif | ||
100 | |||
101 | /* Local functions. */ | ||
102 | local z_crc_t multmodp OF((z_crc_t a, z_crc_t b)); | ||
103 | local z_crc_t x2nmodp OF((z_off64_t n, unsigned k)); | ||
104 | #ifdef W | ||
105 | local z_word_t byte_swap OF((z_word_t word)); | ||
106 | local z_crc_t crc_word OF((z_word_t data)); | ||
107 | local z_word_t crc_word_big OF((z_word_t data)); | ||
108 | #endif /* W */ | ||
71 | 109 | ||
110 | /* CRC polynomial. */ | ||
111 | #define POLY 0xedb88320 /* p(x) reflected, with x^32 implied */ | ||
72 | 112 | ||
73 | #ifdef DYNAMIC_CRC_TABLE | 113 | #ifdef DYNAMIC_CRC_TABLE |
74 | 114 | ||
75 | local volatile int crc_table_empty = 1; | 115 | local volatile int crc_table_empty = 1; |
76 | local z_crc_t FAR crc_table[TBLS][256]; | 116 | local z_crc_t FAR crc_table[256]; |
77 | local z_crc_t FAR crc_comb[GF2_DIM][GF2_DIM]; | 117 | local z_crc_t FAR x2n_table[32]; |
78 | local void make_crc_table OF((void)); | 118 | local void make_crc_table OF((void)); |
79 | local void gf2_matrix_square OF((z_crc_t *square, const z_crc_t *mat)); | 119 | #ifdef W |
120 | local z_word_t FAR crc_big_table[256]; | ||
121 | local z_crc_t FAR crc_braid_table[W][256]; | ||
122 | local z_word_t FAR crc_braid_big_table[W][256]; | ||
123 | local void braid OF((z_crc_t [][256], z_word_t [][256], int, int)); | ||
124 | #endif | ||
80 | #ifdef MAKECRCH | 125 | #ifdef MAKECRCH |
81 | local void write_table OF((FILE *, const z_crc_t FAR *, int)); | 126 | local void write_table OF((FILE *, const z_crc_t FAR *, int)); |
127 | local void write_table32hi OF((FILE *, const z_word_t FAR *, int)); | ||
128 | local void write_table64 OF((FILE *, const z_word_t FAR *, int)); | ||
82 | #endif /* MAKECRCH */ | 129 | #endif /* MAKECRCH */ |
83 | 130 | ||
84 | /* ========================================================================= */ | ||
85 | local void gf2_matrix_square(square, mat) | ||
86 | z_crc_t *square; | ||
87 | const z_crc_t *mat; | ||
88 | { | ||
89 | int n; | ||
90 | |||
91 | for (n = 0; n < GF2_DIM; n++) | ||
92 | square[n] = gf2_matrix_times(mat, mat[n]); | ||
93 | } | ||
94 | |||
95 | /* | 131 | /* |
96 | Generate tables for a byte-wise 32-bit CRC calculation on the polynomial: | 132 | Generate tables for a byte-wise 32-bit CRC calculation on the polynomial: |
97 | x^32+x^26+x^23+x^22+x^16+x^12+x^11+x^10+x^8+x^7+x^5+x^4+x^2+x+1. | 133 | x^32+x^26+x^23+x^22+x^16+x^12+x^11+x^10+x^8+x^7+x^5+x^4+x^2+x+1. |
98 | 134 | ||
99 | Polynomials over GF(2) are represented in binary, one bit per coefficient, | 135 | Polynomials over GF(2) are represented in binary, one bit per coefficient, |
100 | with the lowest powers in the most significant bit. Then adding polynomials | 136 | with the lowest powers in the most significant bit. Then adding polynomials |
101 | is just exclusive-or, and multiplying a polynomial by x is a right shift by | 137 | is just exclusive-or, and multiplying a polynomial by x is a right shift by |
102 | one. If we call the above polynomial p, and represent a byte as the | 138 | one. If we call the above polynomial p, and represent a byte as the |
103 | polynomial q, also with the lowest power in the most significant bit (so the | 139 | polynomial q, also with the lowest power in the most significant bit (so the |
104 | byte 0xb1 is the polynomial x^7+x^3+x+1), then the CRC is (q*x^32) mod p, | 140 | byte 0xb1 is the polynomial x^7+x^3+x+1), then the CRC is (q*x^32) mod p, |
105 | where a mod b means the remainder after dividing a by b. | 141 | where a mod b means the remainder after dividing a by b. |
106 | 142 | ||
107 | This calculation is done using the shift-register method of multiplying and | 143 | This calculation is done using the shift-register method of multiplying and |
108 | taking the remainder. The register is initialized to zero, and for each | 144 | taking the remainder. The register is initialized to zero, and for each |
109 | incoming bit, x^32 is added mod p to the register if the bit is a one (where | 145 | incoming bit, x^32 is added mod p to the register if the bit is a one (where |
110 | x^32 mod p is p+x^32 = x^26+...+1), and the register is multiplied mod p by | 146 | x^32 mod p is p+x^32 = x^26+...+1), and the register is multiplied mod p by x |
111 | x (which is shifting right by one and adding x^32 mod p if the bit shifted | 147 | (which is shifting right by one and adding x^32 mod p if the bit shifted out |
112 | out is a one). We start with the highest power (least significant bit) of | 148 | is a one). We start with the highest power (least significant bit) of q and |
113 | q and repeat for all eight bits of q. | 149 | repeat for all eight bits of q. |
114 | 150 | ||
115 | The first table is simply the CRC of all possible eight bit values. This is | 151 | The table is simply the CRC of all possible eight bit values. This is all the |
116 | all the information needed to generate CRCs on data a byte at a time for all | 152 | information needed to generate CRCs on data a byte at a time for all |
117 | combinations of CRC register values and incoming bytes. The remaining tables | 153 | combinations of CRC register values and incoming bytes. |
118 | allow for word-at-a-time CRC calculation for both big-endian and little- | 154 | */ |
119 | endian machines, where a word is four bytes. | 155 | |
120 | */ | ||
121 | local void make_crc_table() | 156 | local void make_crc_table() |
122 | { | 157 | { |
123 | z_crc_t c; | 158 | z_crc_t p; |
124 | int n, k; | ||
125 | z_crc_t poly; /* polynomial exclusive-or pattern */ | ||
126 | /* terms of polynomial defining this crc (except x^32): */ | ||
127 | static volatile int first = 1; /* flag to limit concurrent making */ | 159 | static volatile int first = 1; /* flag to limit concurrent making */ |
128 | static const unsigned char p[] = {0,1,2,4,5,7,8,10,11,12,16,22,23,26}; | ||
129 | 160 | ||
130 | /* See if another task is already doing this (not thread-safe, but better | 161 | /* See if another task is already doing this (not thread-safe, but better |
131 | than nothing -- significantly reduces duration of vulnerability in | 162 | than nothing -- significantly reduces duration of vulnerability in |
132 | case the advice about DYNAMIC_CRC_TABLE is ignored) */ | 163 | case the advice about DYNAMIC_CRC_TABLE is ignored) */ |
133 | if (first) { | 164 | if (first) { |
165 | unsigned i, j, n; | ||
134 | first = 0; | 166 | first = 0; |
135 | 167 | ||
136 | /* make exclusive-or pattern from polynomial (0xedb88320UL) */ | 168 | /* initialize the CRC of bytes tables */ |
137 | poly = 0; | 169 | for (i = 0; i < 256; i++) { |
138 | for (n = 0; n < (int)(sizeof(p)/sizeof(unsigned char)); n++) | 170 | p = i; |
139 | poly |= (z_crc_t)1 << (31 - p[n]); | 171 | for (j = 0; j < 8; j++) |
140 | 172 | p = p & 1 ? (p >> 1) ^ POLY : p >> 1; | |
141 | /* generate a crc for every 8-bit value */ | 173 | crc_table[i] = p; |
142 | for (n = 0; n < 256; n++) { | 174 | #ifdef W |
143 | c = (z_crc_t)n; | 175 | crc_big_table[i] = byte_swap(p); |
144 | for (k = 0; k < 8; k++) | 176 | #endif |
145 | c = c & 1 ? poly ^ (c >> 1) : c >> 1; | ||
146 | crc_table[0][n] = c; | ||
147 | } | ||
148 | |||
149 | #ifdef BYFOUR | ||
150 | /* generate crc for each value followed by one, two, and three zeros, | ||
151 | and then the byte reversal of those as well as the first table */ | ||
152 | for (n = 0; n < 256; n++) { | ||
153 | c = crc_table[0][n]; | ||
154 | crc_table[4][n] = ZSWAP32(c); | ||
155 | for (k = 1; k < 4; k++) { | ||
156 | c = crc_table[0][c & 0xff] ^ (c >> 8); | ||
157 | crc_table[k][n] = c; | ||
158 | crc_table[k + 4][n] = ZSWAP32(c); | ||
159 | } | ||
160 | } | ||
161 | #endif /* BYFOUR */ | ||
162 | |||
163 | /* generate zero operators table for crc32_combine() */ | ||
164 | |||
165 | /* generate the operator to apply a single zero bit to a CRC -- the | ||
166 | first row adds the polynomial if the low bit is a 1, and the | ||
167 | remaining rows shift the CRC right one bit */ | ||
168 | k = GF2_DIM - 3; | ||
169 | crc_comb[k][0] = 0xedb88320UL; /* CRC-32 polynomial */ | ||
170 | z_crc_t row = 1; | ||
171 | for (n = 1; n < GF2_DIM; n++) { | ||
172 | crc_comb[k][n] = row; | ||
173 | row <<= 1; | ||
174 | } | 177 | } |
175 | 178 | ||
176 | /* generate operators that apply 2, 4, and 8 zeros to a CRC, putting | 179 | /* initialize the x^2^n mod p(x) table */ |
177 | the last one, the operator for one zero byte, at the 0 position */ | 180 | p = (z_crc_t)1 << 30; /* x^1 */ |
178 | gf2_matrix_square(crc_comb[k + 1], crc_comb[k]); | 181 | x2n_table[0] = p; |
179 | gf2_matrix_square(crc_comb[k + 2], crc_comb[k + 1]); | 182 | for (n = 1; n < 32; n++) |
180 | gf2_matrix_square(crc_comb[0], crc_comb[k + 2]); | 183 | x2n_table[n] = p = multmodp(p, p); |
181 | 184 | #ifdef W | |
182 | /* generate operators for applying 2^n zero bytes to a CRC, filling out | 185 | /* initialize the braiding tables -- needs x2n_table[] */ |
183 | the remainder of the table -- the operators repeat after GF2_DIM | 186 | braid(crc_braid_table, crc_braid_big_table, N, W); |
184 | values of n, so the table only needs GF2_DIM entries, regardless of | 187 | #endif |
185 | the size of the length being processed */ | ||
186 | for (n = 1; n < k; n++) | ||
187 | gf2_matrix_square(crc_comb[n], crc_comb[n - 1]); | ||
188 | 188 | ||
189 | /* mark tables as complete, in case someone else is waiting */ | 189 | /* mark tables as complete, in case someone else is waiting */ |
190 | crc_table_empty = 0; | 190 | crc_table_empty = 0; |
@@ -196,42 +196,145 @@ local void make_crc_table() | |||
196 | } | 196 | } |
197 | #ifdef MAKECRCH | 197 | #ifdef MAKECRCH |
198 | { | 198 | { |
199 | /* | ||
200 | The crc32.h header file contains tables for both 32-bit and 64-bit | ||
201 | z_word_t's, and so requires a 64-bit type be available. In that case, | ||
202 | z_word_t must be defined to be 64-bits. This code then also generates | ||
203 | and writes out the tables for the case that z_word_t is 32 bits. | ||
204 | */ | ||
205 | #if !defined(W) || W != 8 | ||
206 | # error Need a 64-bit integer type in order to generate crc32.h. | ||
207 | #endif | ||
199 | FILE *out; | 208 | FILE *out; |
209 | int k, n; | ||
210 | z_crc_t ltl[8][256]; | ||
211 | z_word_t big[8][256]; | ||
200 | 212 | ||
201 | out = fopen("crc32.h", "w"); | 213 | out = fopen("crc32.h", "w"); |
202 | if (out == NULL) return; | 214 | if (out == NULL) return; |
203 | 215 | ||
204 | /* write out CRC table to crc32.h */ | 216 | /* write out little-endian CRC table to crc32.h */ |
205 | fprintf(out, "/* crc32.h -- tables for rapid CRC calculation\n"); | 217 | fprintf(out, |
206 | fprintf(out, " * Generated automatically by crc32.c\n */\n\n"); | 218 | "/* crc32.h -- tables for rapid CRC calculation\n" |
207 | fprintf(out, "local const z_crc_t FAR "); | 219 | " * Generated automatically by crc32.c\n */\n" |
208 | fprintf(out, "crc_table[%d][256] =\n{\n {\n", TBLS); | 220 | "\n" |
209 | write_table(out, crc_table[0], 256); | 221 | "local const z_crc_t FAR crc_table[] = {\n" |
210 | # ifdef BYFOUR | 222 | " "); |
211 | fprintf(out, "#ifdef BYFOUR\n"); | 223 | write_table(out, crc_table, 256); |
212 | for (k = 1; k < 8; k++) { | 224 | fprintf(out, |
213 | fprintf(out, " },\n {\n"); | 225 | "};\n"); |
214 | write_table(out, crc_table[k], 256); | 226 | |
215 | } | 227 | /* write out big-endian CRC table for 64-bit z_word_t to crc32.h */ |
216 | fprintf(out, "#endif\n"); | 228 | fprintf(out, |
217 | # endif /* BYFOUR */ | 229 | "\n" |
218 | fprintf(out, " }\n};\n"); | 230 | "#ifdef W\n" |
219 | 231 | "\n" | |
220 | /* write out zero operator table to crc32.h */ | 232 | "#if W == 8\n" |
221 | fprintf(out, "\nlocal const z_crc_t FAR "); | 233 | "\n" |
222 | fprintf(out, "crc_comb[%d][%d] =\n{\n {\n", GF2_DIM, GF2_DIM); | 234 | "local const z_word_t FAR crc_big_table[] = {\n" |
223 | write_table(out, crc_comb[0], GF2_DIM); | 235 | " "); |
224 | for (k = 1; k < GF2_DIM; k++) { | 236 | write_table64(out, crc_big_table, 256); |
225 | fprintf(out, " },\n {\n"); | 237 | fprintf(out, |
226 | write_table(out, crc_comb[k], GF2_DIM); | 238 | "};\n"); |
239 | |||
240 | /* write out big-endian CRC table for 32-bit z_word_t to crc32.h */ | ||
241 | fprintf(out, | ||
242 | "\n" | ||
243 | "#else /* W == 4 */\n" | ||
244 | "\n" | ||
245 | "local const z_word_t FAR crc_big_table[] = {\n" | ||
246 | " "); | ||
247 | write_table32hi(out, crc_big_table, 256); | ||
248 | fprintf(out, | ||
249 | "};\n" | ||
250 | "\n" | ||
251 | "#endif\n"); | ||
252 | |||
253 | /* write out braid tables for each value of N */ | ||
254 | for (n = 1; n <= 6; n++) { | ||
255 | fprintf(out, | ||
256 | "\n" | ||
257 | "#if N == %d\n", n); | ||
258 | |||
259 | /* compute braid tables for this N and 64-bit word_t */ | ||
260 | braid(ltl, big, n, 8); | ||
261 | |||
262 | /* write out braid tables for 64-bit z_word_t to crc32.h */ | ||
263 | fprintf(out, | ||
264 | "\n" | ||
265 | "#if W == 8\n" | ||
266 | "\n" | ||
267 | "local const z_crc_t FAR crc_braid_table[][256] = {\n"); | ||
268 | for (k = 0; k < 8; k++) { | ||
269 | fprintf(out, " {"); | ||
270 | write_table(out, ltl[k], 256); | ||
271 | fprintf(out, "}%s", k < 7 ? ",\n" : ""); | ||
272 | } | ||
273 | fprintf(out, | ||
274 | "};\n" | ||
275 | "\n" | ||
276 | "local const z_word_t FAR crc_braid_big_table[][256] = {\n"); | ||
277 | for (k = 0; k < 8; k++) { | ||
278 | fprintf(out, " {"); | ||
279 | write_table64(out, big[k], 256); | ||
280 | fprintf(out, "}%s", k < 7 ? ",\n" : ""); | ||
281 | } | ||
282 | fprintf(out, | ||
283 | "};\n"); | ||
284 | |||
285 | /* compute braid tables for this N and 32-bit word_t */ | ||
286 | braid(ltl, big, n, 4); | ||
287 | |||
288 | /* write out braid tables for 32-bit z_word_t to crc32.h */ | ||
289 | fprintf(out, | ||
290 | "\n" | ||
291 | "#else /* W == 4 */\n" | ||
292 | "\n" | ||
293 | "local const z_crc_t FAR crc_braid_table[][256] = {\n"); | ||
294 | for (k = 0; k < 4; k++) { | ||
295 | fprintf(out, " {"); | ||
296 | write_table(out, ltl[k], 256); | ||
297 | fprintf(out, "}%s", k < 3 ? ",\n" : ""); | ||
298 | } | ||
299 | fprintf(out, | ||
300 | "};\n" | ||
301 | "\n" | ||
302 | "local const z_word_t FAR crc_braid_big_table[][256] = {\n"); | ||
303 | for (k = 0; k < 4; k++) { | ||
304 | fprintf(out, " {"); | ||
305 | write_table32hi(out, big[k], 256); | ||
306 | fprintf(out, "}%s", k < 3 ? ",\n" : ""); | ||
307 | } | ||
308 | fprintf(out, | ||
309 | "};\n" | ||
310 | "\n" | ||
311 | "#endif\n" | ||
312 | "\n" | ||
313 | "#endif\n"); | ||
227 | } | 314 | } |
228 | fprintf(out, " }\n};\n"); | 315 | fprintf(out, |
316 | "\n" | ||
317 | "#endif\n"); | ||
318 | |||
319 | /* write out zeros operator table to crc32.h */ | ||
320 | fprintf(out, | ||
321 | "\n" | ||
322 | "local const z_crc_t FAR x2n_table[] = {\n" | ||
323 | " "); | ||
324 | write_table(out, x2n_table, 32); | ||
325 | fprintf(out, | ||
326 | "};\n"); | ||
229 | fclose(out); | 327 | fclose(out); |
230 | } | 328 | } |
231 | #endif /* MAKECRCH */ | 329 | #endif /* MAKECRCH */ |
232 | } | 330 | } |
233 | 331 | ||
234 | #ifdef MAKECRCH | 332 | #ifdef MAKECRCH |
333 | |||
334 | /* | ||
335 | Write the 32-bit values in table[0..k-1] to out, five per line in | ||
336 | hexadecimal separated by commas. | ||
337 | */ | ||
235 | local void write_table(out, table, k) | 338 | local void write_table(out, table, k) |
236 | FILE *out; | 339 | FILE *out; |
237 | const z_crc_t FAR *table; | 340 | const z_crc_t FAR *table; |
@@ -240,26 +343,194 @@ local void write_table(out, table, k) | |||
240 | int n; | 343 | int n; |
241 | 344 | ||
242 | for (n = 0; n < k; n++) | 345 | for (n = 0; n < k; n++) |
243 | fprintf(out, "%s0x%08lxUL%s", n % 5 ? "" : " ", | 346 | fprintf(out, "%s0x%08lx%s", n == 0 || n % 5 ? "" : " ", |
244 | (unsigned long)(table[n]), | 347 | (unsigned long)(table[n]), |
245 | n == k - 1 ? "\n" : (n % 5 == 4 ? ",\n" : ", ")); | 348 | n == k - 1 ? "" : (n % 5 == 4 ? ",\n" : ", ")); |
349 | } | ||
350 | |||
351 | /* | ||
352 | Write the high 32-bits of each value in table[0..k-1] to out, five per line | ||
353 | in hexadecimal separated by commas. | ||
354 | */ | ||
355 | local void write_table32hi(out, table, k) | ||
356 | FILE *out; | ||
357 | const z_word_t FAR *table; | ||
358 | int k; | ||
359 | { | ||
360 | int n; | ||
361 | |||
362 | for (n = 0; n < k; n++) | ||
363 | fprintf(out, "%s0x%08lx%s", n == 0 || n % 5 ? "" : " ", | ||
364 | (unsigned long)(table[n] >> 32), | ||
365 | n == k - 1 ? "" : (n % 5 == 4 ? ",\n" : ", ")); | ||
246 | } | 366 | } |
247 | 367 | ||
368 | /* | ||
369 | Write the 64-bit values in table[0..k-1] to out, three per line in | ||
370 | hexadecimal separated by commas. This assumes that if there is a 64-bit | ||
371 | type, then there is also a long long integer type, and it is at least 64 | ||
372 | bits. If not, then the type cast and format string can be adjusted | ||
373 | accordingly. | ||
374 | */ | ||
375 | local void write_table64(out, table, k) | ||
376 | FILE *out; | ||
377 | const z_word_t FAR *table; | ||
378 | int k; | ||
379 | { | ||
380 | int n; | ||
381 | |||
382 | for (n = 0; n < k; n++) | ||
383 | fprintf(out, "%s0x%016llx%s", n == 0 || n % 3 ? "" : " ", | ||
384 | (unsigned long long)(table[n]), | ||
385 | n == k - 1 ? "" : (n % 3 == 2 ? ",\n" : ", ")); | ||
386 | } | ||
387 | |||
388 | /* Actually do the deed. */ | ||
248 | int main() | 389 | int main() |
249 | { | 390 | { |
250 | make_crc_table(); | 391 | make_crc_table(); |
251 | return 0; | 392 | return 0; |
252 | } | 393 | } |
394 | |||
253 | #endif /* MAKECRCH */ | 395 | #endif /* MAKECRCH */ |
254 | 396 | ||
397 | #ifdef W | ||
398 | /* | ||
399 | Generate the little and big-endian braid tables for the given n and z_word_t | ||
400 | size w. Each array must have room for w blocks of 256 elements. | ||
401 | */ | ||
402 | local void braid(ltl, big, n, w) | ||
403 | z_crc_t ltl[][256]; | ||
404 | z_word_t big[][256]; | ||
405 | int n; | ||
406 | int w; | ||
407 | { | ||
408 | int k; | ||
409 | z_crc_t i, p, q; | ||
410 | for (k = 0; k < w; k++) { | ||
411 | p = x2nmodp((n * w + 3 - k) << 3, 0); | ||
412 | ltl[k][0] = 0; | ||
413 | big[w - 1 - k][0] = 0; | ||
414 | for (i = 1; i < 256; i++) { | ||
415 | ltl[k][i] = q = multmodp(i << 24, p); | ||
416 | big[w - 1 - k][i] = byte_swap(q); | ||
417 | } | ||
418 | } | ||
419 | } | ||
420 | #endif | ||
421 | |||
255 | #else /* !DYNAMIC_CRC_TABLE */ | 422 | #else /* !DYNAMIC_CRC_TABLE */ |
256 | /* ======================================================================== | 423 | /* ======================================================================== |
257 | * Tables of CRC-32s of all single-byte values, made by make_crc_table(), | 424 | * Tables for byte-wise and braided CRC-32 calculations, and a table of powers |
258 | * and tables of zero operator matrices for crc32_combine(). | 425 | * of x for combining CRC-32s, all made by make_crc_table(). |
259 | */ | 426 | */ |
260 | #include "crc32.h" | 427 | #include "crc32.h" |
261 | #endif /* DYNAMIC_CRC_TABLE */ | 428 | #endif /* DYNAMIC_CRC_TABLE */ |
262 | 429 | ||
430 | /* ======================================================================== | ||
431 | * Routines used for CRC calculation. Some are also required for the table | ||
432 | * generation above. | ||
433 | */ | ||
434 | |||
435 | /* | ||
436 | Return a(x) multiplied by b(x) modulo p(x), where p(x) is the CRC polynomial, | ||
437 | reflected. For speed, this requires that a not be zero. | ||
438 | */ | ||
439 | local z_crc_t multmodp(a, b) | ||
440 | z_crc_t a; | ||
441 | z_crc_t b; | ||
442 | { | ||
443 | z_crc_t m, p; | ||
444 | |||
445 | m = (z_crc_t)1 << 31; | ||
446 | p = 0; | ||
447 | for (;;) { | ||
448 | if (a & m) { | ||
449 | p ^= b; | ||
450 | if ((a & (m - 1)) == 0) | ||
451 | break; | ||
452 | } | ||
453 | m >>= 1; | ||
454 | b = b & 1 ? (b >> 1) ^ POLY : b >> 1; | ||
455 | } | ||
456 | return p; | ||
457 | } | ||
458 | |||
459 | /* | ||
460 | Return x^(n+k) modulo p(x). Requires that x2n_table[] has been initialized. | ||
461 | */ | ||
462 | local z_crc_t x2nmodp(n, k) | ||
463 | z_off64_t n; | ||
464 | unsigned k; | ||
465 | { | ||
466 | z_crc_t p; | ||
467 | |||
468 | p = (z_crc_t)1 << 31; /* x^0 == 1 */ | ||
469 | while (n) { | ||
470 | if (n & 1) | ||
471 | p = multmodp(x2n_table[k & 31], p); | ||
472 | n >>= 1; | ||
473 | k++; | ||
474 | } | ||
475 | return p; | ||
476 | } | ||
477 | |||
478 | #ifdef W | ||
479 | |||
480 | /* | ||
481 | Swap the bytes in a z_word_t to convert between little and big endian. Any | ||
482 | self-respecting compiler will optimize this to a single machine byte-swap | ||
483 | instruction, if one is available. This assumes that word_t is either 32 bits | ||
484 | or 64 bits. | ||
485 | */ | ||
486 | local z_word_t byte_swap(word) | ||
487 | z_word_t word; | ||
488 | { | ||
489 | #if W == 8 | ||
490 | return | ||
491 | (word & 0xff00000000000000) >> 56 | | ||
492 | (word & 0xff000000000000) >> 40 | | ||
493 | (word & 0xff0000000000) >> 24 | | ||
494 | (word & 0xff00000000) >> 8 | | ||
495 | (word & 0xff000000) << 8 | | ||
496 | (word & 0xff0000) << 24 | | ||
497 | (word & 0xff00) << 40 | | ||
498 | (word & 0xff) << 56; | ||
499 | #else /* W == 4 */ | ||
500 | return | ||
501 | (word & 0xff000000) >> 24 | | ||
502 | (word & 0xff0000) >> 8 | | ||
503 | (word & 0xff00) << 8 | | ||
504 | (word & 0xff) << 24; | ||
505 | #endif | ||
506 | } | ||
507 | |||
508 | /* | ||
509 | Return the CRC of the W bytes in the word_t data, taking the | ||
510 | least-significant byte of the word as the first byte of data, without any pre | ||
511 | or post conditioning. This is used to combine the CRCs of each braid. | ||
512 | */ | ||
513 | local z_crc_t crc_word(data) | ||
514 | z_word_t data; | ||
515 | { | ||
516 | int k; | ||
517 | for (k = 0; k < W; k++) | ||
518 | data = (data >> 8) ^ crc_table[data & 0xff]; | ||
519 | return (z_crc_t)data; | ||
520 | } | ||
521 | |||
522 | local z_word_t crc_word_big(data) | ||
523 | z_word_t data; | ||
524 | { | ||
525 | int k; | ||
526 | for (k = 0; k < W; k++) | ||
527 | data = (data << 8) ^ | ||
528 | crc_big_table[(data >> ((W - 1) << 3)) & 0xff]; | ||
529 | return data; | ||
530 | } | ||
531 | |||
532 | #endif /* W */ | ||
533 | |||
263 | /* ========================================================================= | 534 | /* ========================================================================= |
264 | * This function can be used by asm versions of crc32() | 535 | * This function can be used by asm versions of crc32() |
265 | */ | 536 | */ |
@@ -273,168 +544,348 @@ const z_crc_t FAR * ZEXPORT get_crc_table() | |||
273 | } | 544 | } |
274 | 545 | ||
275 | /* ========================================================================= */ | 546 | /* ========================================================================= */ |
276 | #define DO1 crc = crc_table[0][((int)crc ^ (*buf++)) & 0xff] ^ (crc >> 8) | ||
277 | #define DO8 DO1; DO1; DO1; DO1; DO1; DO1; DO1; DO1 | ||
278 | |||
279 | /* ========================================================================= */ | ||
280 | unsigned long ZEXPORT crc32_z(crc, buf, len) | 547 | unsigned long ZEXPORT crc32_z(crc, buf, len) |
281 | unsigned long crc; | 548 | unsigned long crc; |
282 | const unsigned char FAR *buf; | 549 | const unsigned char FAR *buf; |
283 | z_size_t len; | 550 | z_size_t len; |
284 | { | 551 | { |
285 | if (buf == Z_NULL) return 0UL; | 552 | /* Return initial CRC, if requested. */ |
553 | if (buf == Z_NULL) return 0; | ||
286 | 554 | ||
287 | #ifdef DYNAMIC_CRC_TABLE | 555 | #ifdef DYNAMIC_CRC_TABLE |
288 | if (crc_table_empty) | 556 | if (crc_table_empty) |
289 | make_crc_table(); | 557 | make_crc_table(); |
290 | #endif /* DYNAMIC_CRC_TABLE */ | 558 | #endif /* DYNAMIC_CRC_TABLE */ |
291 | 559 | ||
292 | #ifdef BYFOUR | 560 | /* Pre-condition the CRC */ |
293 | if (sizeof(void *) == sizeof(z_size_t)) { | 561 | crc ^= 0xffffffff; |
294 | z_crc_t endian; | 562 | |
563 | #ifdef W | ||
564 | |||
565 | /* If provided enough bytes, do a braided CRC calculation. */ | ||
566 | if (len >= N * W + W - 1) { | ||
567 | z_size_t blks; | ||
568 | z_word_t const *words; | ||
569 | unsigned endian; | ||
570 | int k; | ||
571 | |||
572 | /* Compute the CRC up to a z_word_t boundary. */ | ||
573 | while (len && ((z_size_t)buf & (W - 1)) != 0) { | ||
574 | len--; | ||
575 | crc = (crc >> 8) ^ crc_table[(crc ^ *buf++) & 0xff]; | ||
576 | } | ||
577 | |||
578 | /* Compute the CRC on as many N z_word_t blocks as are available. */ | ||
579 | blks = len / (N * W); | ||
580 | len -= blks * N * W; | ||
581 | words = (z_word_t const *)buf; | ||
295 | 582 | ||
583 | /* Do endian check at execution time instead of compile time, since ARM | ||
584 | processors can change the endianess at execution time. If the | ||
585 | compiler knows what the endianess will be, it can optimize out the | ||
586 | check and the unused branch. */ | ||
296 | endian = 1; | 587 | endian = 1; |
297 | if (*((unsigned char *)(&endian))) | 588 | if (*(unsigned char *)&endian) { |
298 | return crc32_little(crc, buf, len); | 589 | /* Little endian. */ |
299 | else | 590 | |
300 | return crc32_big(crc, buf, len); | 591 | z_crc_t crc0; |
301 | } | 592 | z_word_t word0; |
302 | #endif /* BYFOUR */ | 593 | #if N > 1 |
303 | crc = crc ^ 0xffffffffUL; | 594 | z_crc_t crc1; |
304 | while (len >= 8) { | 595 | z_word_t word1; |
305 | DO8; | 596 | #if N > 2 |
306 | len -= 8; | 597 | z_crc_t crc2; |
307 | } | 598 | z_word_t word2; |
308 | if (len) do { | 599 | #if N > 3 |
309 | DO1; | 600 | z_crc_t crc3; |
310 | } while (--len); | 601 | z_word_t word3; |
311 | return crc ^ 0xffffffffUL; | 602 | #if N > 4 |
312 | } | 603 | z_crc_t crc4; |
604 | z_word_t word4; | ||
605 | #if N > 5 | ||
606 | z_crc_t crc5; | ||
607 | z_word_t word5; | ||
608 | #endif | ||
609 | #endif | ||
610 | #endif | ||
611 | #endif | ||
612 | #endif | ||
313 | 613 | ||
314 | /* ========================================================================= */ | 614 | /* Initialize the CRC for each braid. */ |
315 | unsigned long ZEXPORT crc32(crc, buf, len) | 615 | crc0 = crc; |
316 | unsigned long crc; | 616 | #if N > 1 |
317 | const unsigned char FAR *buf; | 617 | crc1 = 0; |
318 | uInt len; | 618 | #if N > 2 |
319 | { | 619 | crc2 = 0; |
320 | return crc32_z(crc, buf, len); | 620 | #if N > 3 |
321 | } | 621 | crc3 = 0; |
622 | #if N > 4 | ||
623 | crc4 = 0; | ||
624 | #if N > 5 | ||
625 | crc5 = 0; | ||
626 | #endif | ||
627 | #endif | ||
628 | #endif | ||
629 | #endif | ||
630 | #endif | ||
322 | 631 | ||
323 | #ifdef BYFOUR | 632 | /* |
633 | Process the first blks-1 blocks, computing the CRCs on each braid | ||
634 | independently. | ||
635 | */ | ||
636 | while (--blks) { | ||
637 | /* Load the word for each braid into registers. */ | ||
638 | word0 = crc0 ^ words[0]; | ||
639 | #if N > 1 | ||
640 | word1 = crc1 ^ words[1]; | ||
641 | #if N > 2 | ||
642 | word2 = crc2 ^ words[2]; | ||
643 | #if N > 3 | ||
644 | word3 = crc3 ^ words[3]; | ||
645 | #if N > 4 | ||
646 | word4 = crc4 ^ words[4]; | ||
647 | #if N > 5 | ||
648 | word5 = crc5 ^ words[5]; | ||
649 | #endif | ||
650 | #endif | ||
651 | #endif | ||
652 | #endif | ||
653 | #endif | ||
654 | words += N; | ||
655 | |||
656 | /* Compute and update the CRC for each word. The loop should | ||
657 | get unrolled. */ | ||
658 | crc0 = crc_braid_table[0][word0 & 0xff]; | ||
659 | #if N > 1 | ||
660 | crc1 = crc_braid_table[0][word1 & 0xff]; | ||
661 | #if N > 2 | ||
662 | crc2 = crc_braid_table[0][word2 & 0xff]; | ||
663 | #if N > 3 | ||
664 | crc3 = crc_braid_table[0][word3 & 0xff]; | ||
665 | #if N > 4 | ||
666 | crc4 = crc_braid_table[0][word4 & 0xff]; | ||
667 | #if N > 5 | ||
668 | crc5 = crc_braid_table[0][word5 & 0xff]; | ||
669 | #endif | ||
670 | #endif | ||
671 | #endif | ||
672 | #endif | ||
673 | #endif | ||
674 | for (k = 1; k < W; k++) { | ||
675 | crc0 ^= crc_braid_table[k][(word0 >> (k << 3)) & 0xff]; | ||
676 | #if N > 1 | ||
677 | crc1 ^= crc_braid_table[k][(word1 >> (k << 3)) & 0xff]; | ||
678 | #if N > 2 | ||
679 | crc2 ^= crc_braid_table[k][(word2 >> (k << 3)) & 0xff]; | ||
680 | #if N > 3 | ||
681 | crc3 ^= crc_braid_table[k][(word3 >> (k << 3)) & 0xff]; | ||
682 | #if N > 4 | ||
683 | crc4 ^= crc_braid_table[k][(word4 >> (k << 3)) & 0xff]; | ||
684 | #if N > 5 | ||
685 | crc5 ^= crc_braid_table[k][(word5 >> (k << 3)) & 0xff]; | ||
686 | #endif | ||
687 | #endif | ||
688 | #endif | ||
689 | #endif | ||
690 | #endif | ||
691 | } | ||
692 | } | ||
324 | 693 | ||
325 | /* | 694 | /* |
326 | This BYFOUR code accesses the passed unsigned char * buffer with a 32-bit | 695 | Process the last block, combining the CRCs of the N braids at the |
327 | integer pointer type. This violates the strict aliasing rule, where a | 696 | same time. |
328 | compiler can assume, for optimization purposes, that two pointers to | 697 | */ |
329 | fundamentally different types won't ever point to the same memory. This can | 698 | crc = crc_word(crc0 ^ words[0]); |
330 | manifest as a problem only if one of the pointers is written to. This code | 699 | #if N > 1 |
331 | only reads from those pointers. So long as this code remains isolated in | 700 | crc = crc_word(crc1 ^ words[1] ^ crc); |
332 | this compilation unit, there won't be a problem. For this reason, this code | 701 | #if N > 2 |
333 | should not be copied and pasted into a compilation unit in which other code | 702 | crc = crc_word(crc2 ^ words[2] ^ crc); |
334 | writes to the buffer that is passed to these routines. | 703 | #if N > 3 |
335 | */ | 704 | crc = crc_word(crc3 ^ words[3] ^ crc); |
705 | #if N > 4 | ||
706 | crc = crc_word(crc4 ^ words[4] ^ crc); | ||
707 | #if N > 5 | ||
708 | crc = crc_word(crc5 ^ words[5] ^ crc); | ||
709 | #endif | ||
710 | #endif | ||
711 | #endif | ||
712 | #endif | ||
713 | #endif | ||
714 | words += N; | ||
715 | } | ||
716 | else { | ||
717 | /* Big endian. */ | ||
718 | |||
719 | z_word_t crc0, word0, comb; | ||
720 | #if N > 1 | ||
721 | z_word_t crc1, word1; | ||
722 | #if N > 2 | ||
723 | z_word_t crc2, word2; | ||
724 | #if N > 3 | ||
725 | z_word_t crc3, word3; | ||
726 | #if N > 4 | ||
727 | z_word_t crc4, word4; | ||
728 | #if N > 5 | ||
729 | z_word_t crc5, word5; | ||
730 | #endif | ||
731 | #endif | ||
732 | #endif | ||
733 | #endif | ||
734 | #endif | ||
336 | 735 | ||
337 | /* ========================================================================= */ | 736 | /* Initialize the CRC for each braid. */ |
338 | #define DOLIT4 c ^= *buf4++; \ | 737 | crc0 = byte_swap(crc); |
339 | c = crc_table[3][c & 0xff] ^ crc_table[2][(c >> 8) & 0xff] ^ \ | 738 | #if N > 1 |
340 | crc_table[1][(c >> 16) & 0xff] ^ crc_table[0][c >> 24] | 739 | crc1 = 0; |
341 | #define DOLIT32 DOLIT4; DOLIT4; DOLIT4; DOLIT4; DOLIT4; DOLIT4; DOLIT4; DOLIT4 | 740 | #if N > 2 |
741 | crc2 = 0; | ||
742 | #if N > 3 | ||
743 | crc3 = 0; | ||
744 | #if N > 4 | ||
745 | crc4 = 0; | ||
746 | #if N > 5 | ||
747 | crc5 = 0; | ||
748 | #endif | ||
749 | #endif | ||
750 | #endif | ||
751 | #endif | ||
752 | #endif | ||
342 | 753 | ||
343 | /* ========================================================================= */ | 754 | /* |
344 | local unsigned long crc32_little(crc, buf, len) | 755 | Process the first blks-1 blocks, computing the CRCs on each braid |
345 | unsigned long crc; | 756 | independently. |
346 | const unsigned char FAR *buf; | 757 | */ |
347 | z_size_t len; | 758 | while (--blks) { |
348 | { | 759 | /* Load the word for each braid into registers. */ |
349 | register z_crc_t c; | 760 | word0 = crc0 ^ words[0]; |
350 | register const z_crc_t FAR *buf4; | 761 | #if N > 1 |
762 | word1 = crc1 ^ words[1]; | ||
763 | #if N > 2 | ||
764 | word2 = crc2 ^ words[2]; | ||
765 | #if N > 3 | ||
766 | word3 = crc3 ^ words[3]; | ||
767 | #if N > 4 | ||
768 | word4 = crc4 ^ words[4]; | ||
769 | #if N > 5 | ||
770 | word5 = crc5 ^ words[5]; | ||
771 | #endif | ||
772 | #endif | ||
773 | #endif | ||
774 | #endif | ||
775 | #endif | ||
776 | words += N; | ||
777 | |||
778 | /* Compute and update the CRC for each word. The loop should | ||
779 | get unrolled. */ | ||
780 | crc0 = crc_braid_big_table[0][word0 & 0xff]; | ||
781 | #if N > 1 | ||
782 | crc1 = crc_braid_big_table[0][word1 & 0xff]; | ||
783 | #if N > 2 | ||
784 | crc2 = crc_braid_big_table[0][word2 & 0xff]; | ||
785 | #if N > 3 | ||
786 | crc3 = crc_braid_big_table[0][word3 & 0xff]; | ||
787 | #if N > 4 | ||
788 | crc4 = crc_braid_big_table[0][word4 & 0xff]; | ||
789 | #if N > 5 | ||
790 | crc5 = crc_braid_big_table[0][word5 & 0xff]; | ||
791 | #endif | ||
792 | #endif | ||
793 | #endif | ||
794 | #endif | ||
795 | #endif | ||
796 | for (k = 1; k < W; k++) { | ||
797 | crc0 ^= crc_braid_big_table[k][(word0 >> (k << 3)) & 0xff]; | ||
798 | #if N > 1 | ||
799 | crc1 ^= crc_braid_big_table[k][(word1 >> (k << 3)) & 0xff]; | ||
800 | #if N > 2 | ||
801 | crc2 ^= crc_braid_big_table[k][(word2 >> (k << 3)) & 0xff]; | ||
802 | #if N > 3 | ||
803 | crc3 ^= crc_braid_big_table[k][(word3 >> (k << 3)) & 0xff]; | ||
804 | #if N > 4 | ||
805 | crc4 ^= crc_braid_big_table[k][(word4 >> (k << 3)) & 0xff]; | ||
806 | #if N > 5 | ||
807 | crc5 ^= crc_braid_big_table[k][(word5 >> (k << 3)) & 0xff]; | ||
808 | #endif | ||
809 | #endif | ||
810 | #endif | ||
811 | #endif | ||
812 | #endif | ||
813 | } | ||
814 | } | ||
351 | 815 | ||
352 | c = (z_crc_t)crc; | 816 | /* |
353 | c = ~c; | 817 | Process the last block, combining the CRCs of the N braids at the |
354 | while (len && ((z_size_t)buf & 3)) { | 818 | same time. |
355 | c = crc_table[0][(c ^ *buf++) & 0xff] ^ (c >> 8); | 819 | */ |
356 | len--; | 820 | comb = crc_word_big(crc0 ^ words[0]); |
821 | #if N > 1 | ||
822 | comb = crc_word_big(crc1 ^ words[1] ^ comb); | ||
823 | #if N > 2 | ||
824 | comb = crc_word_big(crc2 ^ words[2] ^ comb); | ||
825 | #if N > 3 | ||
826 | comb = crc_word_big(crc3 ^ words[3] ^ comb); | ||
827 | #if N > 4 | ||
828 | comb = crc_word_big(crc4 ^ words[4] ^ comb); | ||
829 | #if N > 5 | ||
830 | comb = crc_word_big(crc5 ^ words[5] ^ comb); | ||
831 | #endif | ||
832 | #endif | ||
833 | #endif | ||
834 | #endif | ||
835 | #endif | ||
836 | words += N; | ||
837 | crc = byte_swap(comb); | ||
838 | } | ||
839 | |||
840 | /* | ||
841 | Update the pointer to the remaining bytes to process. | ||
842 | */ | ||
843 | buf = (unsigned char const *)words; | ||
357 | } | 844 | } |
358 | 845 | ||
359 | buf4 = (const z_crc_t FAR *)(const void FAR *)buf; | 846 | #endif /* W */ |
360 | while (len >= 32) { | 847 | |
361 | DOLIT32; | 848 | /* Complete the computation of the CRC on any remaining bytes. */ |
362 | len -= 32; | 849 | while (len >= 8) { |
850 | len -= 8; | ||
851 | crc = (crc >> 8) ^ crc_table[(crc ^ *buf++) & 0xff]; | ||
852 | crc = (crc >> 8) ^ crc_table[(crc ^ *buf++) & 0xff]; | ||
853 | crc = (crc >> 8) ^ crc_table[(crc ^ *buf++) & 0xff]; | ||
854 | crc = (crc >> 8) ^ crc_table[(crc ^ *buf++) & 0xff]; | ||
855 | crc = (crc >> 8) ^ crc_table[(crc ^ *buf++) & 0xff]; | ||
856 | crc = (crc >> 8) ^ crc_table[(crc ^ *buf++) & 0xff]; | ||
857 | crc = (crc >> 8) ^ crc_table[(crc ^ *buf++) & 0xff]; | ||
858 | crc = (crc >> 8) ^ crc_table[(crc ^ *buf++) & 0xff]; | ||
363 | } | 859 | } |
364 | while (len >= 4) { | 860 | while (len) { |
365 | DOLIT4; | 861 | len--; |
366 | len -= 4; | 862 | crc = (crc >> 8) ^ crc_table[(crc ^ *buf++) & 0xff]; |
367 | } | 863 | } |
368 | buf = (const unsigned char FAR *)buf4; | ||
369 | 864 | ||
370 | if (len) do { | 865 | /* Return the CRC, post-conditioned. */ |
371 | c = crc_table[0][(c ^ *buf++) & 0xff] ^ (c >> 8); | 866 | return crc ^ 0xffffffff; |
372 | } while (--len); | ||
373 | c = ~c; | ||
374 | return (unsigned long)c; | ||
375 | } | 867 | } |
376 | 868 | ||
377 | /* ========================================================================= */ | 869 | /* ========================================================================= */ |
378 | #define DOBIG4 c ^= *buf4++; \ | 870 | unsigned long ZEXPORT crc32(crc, buf, len) |
379 | c = crc_table[4][c & 0xff] ^ crc_table[5][(c >> 8) & 0xff] ^ \ | ||
380 | crc_table[6][(c >> 16) & 0xff] ^ crc_table[7][c >> 24] | ||
381 | #define DOBIG32 DOBIG4; DOBIG4; DOBIG4; DOBIG4; DOBIG4; DOBIG4; DOBIG4; DOBIG4 | ||
382 | |||
383 | /* ========================================================================= */ | ||
384 | local unsigned long crc32_big(crc, buf, len) | ||
385 | unsigned long crc; | 871 | unsigned long crc; |
386 | const unsigned char FAR *buf; | 872 | const unsigned char FAR *buf; |
387 | z_size_t len; | 873 | uInt len; |
388 | { | 874 | { |
389 | register z_crc_t c; | 875 | return crc32_z(crc, buf, len); |
390 | register const z_crc_t FAR *buf4; | ||
391 | |||
392 | c = ZSWAP32((z_crc_t)crc); | ||
393 | c = ~c; | ||
394 | while (len && ((z_size_t)buf & 3)) { | ||
395 | c = crc_table[4][(c >> 24) ^ *buf++] ^ (c << 8); | ||
396 | len--; | ||
397 | } | ||
398 | |||
399 | buf4 = (const z_crc_t FAR *)(const void FAR *)buf; | ||
400 | while (len >= 32) { | ||
401 | DOBIG32; | ||
402 | len -= 32; | ||
403 | } | ||
404 | while (len >= 4) { | ||
405 | DOBIG4; | ||
406 | len -= 4; | ||
407 | } | ||
408 | buf = (const unsigned char FAR *)buf4; | ||
409 | |||
410 | if (len) do { | ||
411 | c = crc_table[4][(c >> 24) ^ *buf++] ^ (c << 8); | ||
412 | } while (--len); | ||
413 | c = ~c; | ||
414 | return (unsigned long)(ZSWAP32(c)); | ||
415 | } | 876 | } |
416 | 877 | ||
417 | #endif /* BYFOUR */ | ||
418 | |||
419 | /* ========================================================================= */ | 878 | /* ========================================================================= */ |
420 | local uLong crc32_combine_(crc1, crc2, len2) | 879 | uLong ZEXPORT crc32_combine64(crc1, crc2, len2) |
421 | uLong crc1; | 880 | uLong crc1; |
422 | uLong crc2; | 881 | uLong crc2; |
423 | z_off64_t len2; | 882 | z_off64_t len2; |
424 | { | 883 | { |
425 | int n; | ||
426 | |||
427 | #ifdef DYNAMIC_CRC_TABLE | 884 | #ifdef DYNAMIC_CRC_TABLE |
428 | if (crc_table_empty) | 885 | if (crc_table_empty) |
429 | make_crc_table(); | 886 | make_crc_table(); |
430 | #endif /* DYNAMIC_CRC_TABLE */ | 887 | #endif /* DYNAMIC_CRC_TABLE */ |
431 | 888 | return multmodp(x2nmodp(len2, 3), crc1) ^ crc2; | |
432 | if (len2 > 0) | ||
433 | /* operator for 2^n zeros repeats every GF2_DIM n values */ | ||
434 | for (n = 0; len2; n = (n + 1) % GF2_DIM, len2 >>= 1) | ||
435 | if (len2 & 1) | ||
436 | crc1 = gf2_matrix_times(crc_comb[n], crc1); | ||
437 | return crc1 ^ crc2; | ||
438 | } | 889 | } |
439 | 890 | ||
440 | /* ========================================================================= */ | 891 | /* ========================================================================= */ |
@@ -443,87 +894,32 @@ uLong ZEXPORT crc32_combine(crc1, crc2, len2) | |||
443 | uLong crc2; | 894 | uLong crc2; |
444 | z_off_t len2; | 895 | z_off_t len2; |
445 | { | 896 | { |
446 | return crc32_combine_(crc1, crc2, len2); | 897 | return crc32_combine64(crc1, crc2, len2); |
447 | } | ||
448 | |||
449 | uLong ZEXPORT crc32_combine64(crc1, crc2, len2) | ||
450 | uLong crc1; | ||
451 | uLong crc2; | ||
452 | z_off64_t len2; | ||
453 | { | ||
454 | return crc32_combine_(crc1, crc2, len2); | ||
455 | } | 898 | } |
456 | 899 | ||
457 | /* ========================================================================= */ | 900 | /* ========================================================================= */ |
458 | local void crc32_combine_gen_(op, len2) | 901 | uLong ZEXPORT crc32_combine_gen64(len2) |
459 | z_crc_t *op; | ||
460 | z_off64_t len2; | 902 | z_off64_t len2; |
461 | { | 903 | { |
462 | z_crc_t row; | ||
463 | int j; | ||
464 | unsigned i; | ||
465 | |||
466 | #ifdef DYNAMIC_CRC_TABLE | 904 | #ifdef DYNAMIC_CRC_TABLE |
467 | if (crc_table_empty) | 905 | if (crc_table_empty) |
468 | make_crc_table(); | 906 | make_crc_table(); |
469 | #endif /* DYNAMIC_CRC_TABLE */ | 907 | #endif /* DYNAMIC_CRC_TABLE */ |
470 | 908 | return x2nmodp(len2, 3); | |
471 | /* if len2 is zero or negative, return the identity matrix */ | ||
472 | if (len2 <= 0) { | ||
473 | row = 1; | ||
474 | for (j = 0; j < GF2_DIM; j++) { | ||
475 | op[j] = row; | ||
476 | row <<= 1; | ||
477 | } | ||
478 | return; | ||
479 | } | ||
480 | |||
481 | /* at least one bit in len2 is set -- find it, and copy the operator | ||
482 | corresponding to that position into op */ | ||
483 | i = 0; | ||
484 | for (;;) { | ||
485 | if (len2 & 1) { | ||
486 | for (j = 0; j < GF2_DIM; j++) | ||
487 | op[j] = crc_comb[i][j]; | ||
488 | break; | ||
489 | } | ||
490 | len2 >>= 1; | ||
491 | i = (i + 1) % GF2_DIM; | ||
492 | } | ||
493 | |||
494 | /* for each remaining bit set in len2 (if any), multiply op by the operator | ||
495 | corresponding to that position */ | ||
496 | for (;;) { | ||
497 | len2 >>= 1; | ||
498 | i = (i + 1) % GF2_DIM; | ||
499 | if (len2 == 0) | ||
500 | break; | ||
501 | if (len2 & 1) | ||
502 | for (j = 0; j < GF2_DIM; j++) | ||
503 | op[j] = gf2_matrix_times(crc_comb[i], op[j]); | ||
504 | } | ||
505 | } | 909 | } |
506 | 910 | ||
507 | /* ========================================================================= */ | 911 | /* ========================================================================= */ |
508 | void ZEXPORT crc32_combine_gen(op, len2) | 912 | uLong ZEXPORT crc32_combine_gen(len2) |
509 | z_crc_t *op; | ||
510 | z_off_t len2; | 913 | z_off_t len2; |
511 | { | 914 | { |
512 | crc32_combine_gen_(op, len2); | 915 | return crc32_combine_gen64(len2); |
513 | } | ||
514 | |||
515 | void ZEXPORT crc32_combine_gen64(op, len2) | ||
516 | z_crc_t *op; | ||
517 | z_off64_t len2; | ||
518 | { | ||
519 | crc32_combine_gen_(op, len2); | ||
520 | } | 916 | } |
521 | 917 | ||
522 | /* ========================================================================= */ | 918 | /* ========================================================================= */ |
523 | uLong crc32_combine_op(crc1, crc2, op) | 919 | uLong crc32_combine_op(crc1, crc2, op) |
524 | uLong crc1; | 920 | uLong crc1; |
525 | uLong crc2; | 921 | uLong crc2; |
526 | const z_crc_t *op; | 922 | uLong op; |
527 | { | 923 | { |
528 | return gf2_matrix_times(op, crc1) ^ crc2; | 924 | return multmodp(op, crc1) ^ crc2; |
529 | } | 925 | } |