/work/libdeflate-src/lib/crc32.c
Line | Count | Source |
1 | | /* |
2 | | * crc32.c - CRC-32 checksum algorithm for the gzip format |
3 | | * |
4 | | * Copyright 2016 Eric Biggers |
5 | | * |
6 | | * Permission is hereby granted, free of charge, to any person |
7 | | * obtaining a copy of this software and associated documentation |
8 | | * files (the "Software"), to deal in the Software without |
9 | | * restriction, including without limitation the rights to use, |
10 | | * copy, modify, merge, publish, distribute, sublicense, and/or sell |
11 | | * copies of the Software, and to permit persons to whom the |
12 | | * Software is furnished to do so, subject to the following |
13 | | * conditions: |
14 | | * |
15 | | * The above copyright notice and this permission notice shall be |
16 | | * included in all copies or substantial portions of the Software. |
17 | | * |
18 | | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
19 | | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES |
20 | | * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
21 | | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT |
22 | | * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, |
23 | | * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
24 | | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
25 | | * OTHER DEALINGS IN THE SOFTWARE. |
26 | | */ |
27 | | |
28 | | /* |
29 | | * High-level description of CRC |
30 | | * ============================= |
31 | | * |
32 | | * Consider a bit sequence 'bits[1...len]'. Interpret 'bits' as the "message" |
33 | | * polynomial M(x) with coefficients in GF(2) (the field of integers modulo 2), |
34 | | * where the coefficient of 'x^i' is 'bits[len - i]'. Then, compute: |
35 | | * |
36 | | * R(x) = M(x)*x^n mod G(x) |
37 | | * |
38 | | * where G(x) is a selected "generator" polynomial of degree 'n'. The remainder |
39 | | * R(x) is a polynomial of max degree 'n - 1'. The CRC of 'bits' is R(x) |
40 | | * interpreted as a bitstring of length 'n'. |
41 | | * |
42 | | * CRC used in gzip |
43 | | * ================ |
44 | | * |
45 | | * In the gzip format (RFC 1952): |
46 | | * |
47 | | * - The bitstring to checksum is formed from the bytes of the uncompressed |
48 | | * data by concatenating the bits from the bytes in order, proceeding |
49 | | * from the low-order bit to the high-order bit within each byte. |
50 | | * |
51 | | * - The generator polynomial G(x) is: x^32 + x^26 + x^23 + x^22 + x^16 + |
52 | | * x^12 + x^11 + x^10 + x^8 + x^7 + x^5 + x^4 + x^2 + x + 1. |
53 | | * Consequently, the CRC length is 32 bits ("CRC-32"). |
54 | | * |
55 | | * - The highest order 32 coefficients of M(x)*x^n are inverted. |
56 | | * |
57 | | * - All 32 coefficients of R(x) are inverted. |
58 | | * |
59 | | * The two inversions cause added leading and trailing zero bits to affect the |
60 | | * resulting CRC, whereas with a regular CRC such bits would have no effect on |
61 | | * the CRC. |
62 | | * |
63 | | * Computation and optimizations |
64 | | * ============================= |
65 | | * |
66 | | * We can compute R(x) through "long division", maintaining only 32 bits of |
67 | | * state at any given time. Multiplication by 'x' can be implemented as |
68 | | * right-shifting by 1 (assuming the polynomial<=>bitstring mapping where the |
69 | | * highest order bit represents the coefficient of x^0), and both addition and |
70 | | * subtraction can be implemented as bitwise exclusive OR (since we are working |
71 | | * in GF(2)). Here is an unoptimized implementation: |
72 | | * |
73 | | * static u32 crc32_gzip(const u8 *p, size_t len) |
74 | | * { |
75 | | * u32 crc = 0; |
76 | | * const u32 divisor = 0xEDB88320; |
77 | | * |
78 | | * for (size_t i = 0; i < len * 8 + 32; i++) { |
79 | | * int bit; |
80 | | * u32 multiple; |
81 | | * |
82 | | * if (i < len * 8) |
83 | | * bit = (p[i / 8] >> (i % 8)) & 1; |
84 | | * else |
85 | | * bit = 0; // one of the 32 appended 0 bits |
86 | | * |
87 | | * if (i < 32) // the first 32 bits are inverted |
88 | | * bit ^= 1; |
89 | | * |
90 | | * if (crc & 1) |
91 | | * multiple = divisor; |
92 | | * else |
93 | | * multiple = 0; |
94 | | * |
95 | | * crc >>= 1; |
96 | | * crc |= (u32)bit << 31; |
97 | | * crc ^= multiple; |
98 | | * } |
99 | | * |
100 | | * return ~crc; |
101 | | * } |
102 | | * |
103 | | * In this implementation, the 32-bit integer 'crc' maintains the remainder of |
104 | | * the currently processed portion of the message (with 32 zero bits appended) |
105 | | * when divided by the generator polynomial. 'crc' is the representation of |
106 | | * R(x), and 'divisor' is the representation of G(x) excluding the x^32 |
107 | | * coefficient. For each bit to process, we multiply R(x) by 'x^1', then add |
108 | | * 'x^0' if the new bit is a 1. If this causes R(x) to gain a nonzero x^32 |
109 | | * term, then we subtract G(x) from R(x). |
110 | | * |
111 | | * We can speed this up by taking advantage of the fact that XOR is commutative |
112 | | * and associative, so the order in which we combine the inputs into 'crc' is |
113 | | * unimportant. And since each message bit we add doesn't affect the choice of |
114 | | * 'multiple' until 32 bits later, we need not actually add each message bit |
115 | | * until that point: |
116 | | * |
117 | | * static u32 crc32_gzip(const u8 *p, size_t len) |
118 | | * { |
119 | | * u32 crc = ~0; |
120 | | * const u32 divisor = 0xEDB88320; |
121 | | * |
122 | | * for (size_t i = 0; i < len * 8; i++) { |
123 | | * int bit; |
124 | | * u32 multiple; |
125 | | * |
126 | | * bit = (p[i / 8] >> (i % 8)) & 1; |
127 | | * crc ^= bit; |
128 | | * if (crc & 1) |
129 | | * multiple = divisor; |
130 | | * else |
131 | | * multiple = 0; |
132 | | * crc >>= 1; |
133 | | * crc ^= multiple; |
134 | | * } |
135 | | * |
136 | | * return ~crc; |
137 | | * } |
138 | | * |
139 | | * With the above implementation we get the effect of 32 appended 0 bits for |
140 | | * free; they never affect the choice of a divisor, nor would they change the |
141 | | * value of 'crc' if they were to be actually XOR'ed in. And by starting with a |
142 | | * remainder of all 1 bits, we get the effect of complementing the first 32 |
143 | | * message bits. |
144 | | * |
145 | | * The next optimization is to process the input in multi-bit units. Suppose |
146 | | * that we insert the next 'n' message bits into the remainder. Then we get an |
147 | | * intermediate remainder of length '32 + n' bits, and the CRC of the extra 'n' |
148 | | * bits is the amount by which the low 32 bits of the remainder will change as a |
149 | | * result of cancelling out those 'n' bits. Taking n=8 (one byte) and |
150 | | * precomputing a table containing the CRC of each possible byte, we get |
151 | | * crc32_slice1() defined below. |
152 | | * |
153 | | * As a further optimization, we could increase the multi-bit unit size to 16. |
154 | | * However, that is inefficient because the table size explodes from 256 entries |
155 | | * (1024 bytes) to 65536 entries (262144 bytes), which wastes memory and won't |
156 | | * fit in L1 cache on typical processors. |
157 | | * |
158 | | * However, we can actually process 4 bytes at a time using 4 different tables |
159 | | * with 256 entries each. Logically, we form a 64-bit intermediate remainder |
160 | | * and cancel out the high 32 bits in 8-bit chunks. Bits 32-39 are cancelled |
161 | | * out by the CRC of those bits, whereas bits 40-47 are be cancelled out by the |
162 | | * CRC of those bits with 8 zero bits appended, and so on. |
163 | | * |
164 | | * In crc32_slice8(), this method is extended to 8 bytes at a time. The |
165 | | * intermediate remainder (which we never actually store explicitly) is 96 bits. |
166 | | * |
167 | | * On CPUs that support fast carryless multiplication, CRCs can be computed even |
168 | | * more quickly via "folding". See e.g. the x86 PCLMUL implementations. |
169 | | */ |
170 | | |
171 | | #include "lib_common.h" |
172 | | #include "crc32_multipliers.h" |
173 | | #include "crc32_tables.h" |
174 | | |
175 | | /* This is the default implementation. It uses the slice-by-8 method. */ |
176 | | static u32 MAYBE_UNUSED |
177 | | crc32_slice8(u32 crc, const u8 *p, size_t len) |
178 | 0 | { |
179 | 0 | const u8 * const end = p + len; |
180 | 0 | const u8 *end64; |
181 | |
|
182 | 0 | for (; ((uintptr_t)p & 7) && p != end; p++) |
183 | 0 | crc = (crc >> 8) ^ crc32_slice8_table[(u8)crc ^ *p]; |
184 | |
|
185 | 0 | end64 = p + ((end - p) & ~7); |
186 | 0 | for (; p != end64; p += 8) { |
187 | 0 | u32 v1 = le32_bswap(*(const u32 *)(p + 0)); |
188 | 0 | u32 v2 = le32_bswap(*(const u32 *)(p + 4)); |
189 | |
|
190 | 0 | crc = crc32_slice8_table[0x700 + (u8)((crc ^ v1) >> 0)] ^ |
191 | 0 | crc32_slice8_table[0x600 + (u8)((crc ^ v1) >> 8)] ^ |
192 | 0 | crc32_slice8_table[0x500 + (u8)((crc ^ v1) >> 16)] ^ |
193 | 0 | crc32_slice8_table[0x400 + (u8)((crc ^ v1) >> 24)] ^ |
194 | 0 | crc32_slice8_table[0x300 + (u8)(v2 >> 0)] ^ |
195 | 0 | crc32_slice8_table[0x200 + (u8)(v2 >> 8)] ^ |
196 | 0 | crc32_slice8_table[0x100 + (u8)(v2 >> 16)] ^ |
197 | 0 | crc32_slice8_table[0x000 + (u8)(v2 >> 24)]; |
198 | 0 | } |
199 | |
|
200 | 0 | for (; p != end; p++) |
201 | 0 | crc = (crc >> 8) ^ crc32_slice8_table[(u8)crc ^ *p]; |
202 | |
|
203 | 0 | return crc; |
204 | 0 | } |
205 | | |
206 | | /* |
207 | | * This is a more lightweight generic implementation, which can be used as a |
208 | | * subroutine by architecture-specific implementations to process small amounts |
209 | | * of unaligned data at the beginning and/or end of the buffer. |
210 | | */ |
211 | | static forceinline u32 MAYBE_UNUSED |
212 | | crc32_slice1(u32 crc, const u8 *p, size_t len) |
213 | 98 | { |
214 | 98 | size_t i; |
215 | | |
216 | 192 | for (i = 0; i < len; i++) |
217 | 94 | crc = (crc >> 8) ^ crc32_slice1_table[(u8)crc ^ p[i]]; |
218 | 98 | return crc; |
219 | 98 | } |
220 | | |
221 | | /* Include architecture-specific implementation(s) if available. */ |
222 | | #undef DEFAULT_IMPL |
223 | | #undef arch_select_crc32_func |
224 | | typedef u32 (*crc32_func_t)(u32 crc, const u8 *p, size_t len); |
225 | | #if defined(ARCH_ARM32) || defined(ARCH_ARM64) |
226 | | # include "arm/crc32_impl.h" |
227 | | #elif defined(ARCH_X86_32) || defined(ARCH_X86_64) |
228 | | # include "x86/crc32_impl.h" |
229 | | #endif |
230 | | |
231 | | #ifndef DEFAULT_IMPL |
232 | 0 | # define DEFAULT_IMPL crc32_slice8 |
233 | | #endif |
234 | | |
235 | | #ifdef arch_select_crc32_func |
236 | | static u32 dispatch_crc32(u32 crc, const u8 *p, size_t len); |
237 | | |
238 | | static volatile crc32_func_t crc32_impl = dispatch_crc32; |
239 | | |
240 | | /* Choose the best implementation at runtime. */ |
241 | | static u32 dispatch_crc32(u32 crc, const u8 *p, size_t len) |
242 | 2 | { |
243 | 2 | crc32_func_t f = arch_select_crc32_func(); |
244 | | |
245 | 2 | if (f == NULL) |
246 | 0 | f = DEFAULT_IMPL; |
247 | | |
248 | 2 | crc32_impl = f; |
249 | 2 | return f(crc, p, len); |
250 | 2 | } |
251 | | #else |
252 | | /* The best implementation is statically known, so call it directly. */ |
253 | | #define crc32_impl DEFAULT_IMPL |
254 | | #endif |
255 | | |
256 | | LIBDEFLATEAPI u32 |
257 | | libdeflate_crc32(u32 crc, const void *p, size_t len) |
258 | 216 | { |
259 | 216 | if (p == NULL) /* Return initial value. */ |
260 | 0 | return 0; |
261 | 216 | return ~crc32_impl(~crc, p, len); |
262 | 216 | } |