MIDAS
Loading...
Searching...
No Matches
crc32c.cxx
Go to the documentation of this file.
1//
2// CRC32C code taken from
3// http://stackoverflow.com/questions/17645167/implementing-sse-4-2s-crc32c-in-software/17646775#17646775
4// on 29 July 2015
5//
6// See also:
7// https://tools.ietf.org/html/rfc3309
8// https://tools.ietf.org/html/rfc3720#appendix-B.4
9// and
10// http://stackoverflow.com/questions/20963944/test-vectors-for-crc32c
11//
12
13#include "crc32c.h"
14
15#if defined(__SSE__) && defined(__x86_64__)
16#define HAVE_AMD64_CRC32C 1
17#endif
18
19#ifdef __ARM_FEATURE_CRC32
20#include <arm_acle.h>
21#define HAVE_ARM64_CRC32C 1
22#endif
23
24/* crc32c.c -- compute CRC-32C using the Intel crc32 instruction
25 * Copyright (C) 2013 Mark Adler
26 * Version 1.1 1 Aug 2013 Mark Adler
27 */
28
29/*
30 This software is provided 'as-is', without any express or implied
31 warranty. In no event will the author be held liable for any damages
32 arising from the use of this software.
33
34 Permission is granted to anyone to use this software for any purpose,
35 including commercial applications, and to alter it and redistribute it
36 freely, subject to the following restrictions:
37
38 1. The origin of this software must not be misrepresented; you must not
39 claim that you wrote the original software. If you use this software
40 in a product, an acknowledgment in the product documentation would be
41 appreciated but is not required.
42 2. Altered source versions must be plainly marked as such, and must not be
43 misrepresented as being the original software.
44 3. This notice may not be removed or altered from any source distribution.
45
46 Mark Adler
47 madler@alumni.caltech.edu
48 */
49
50/* Use hardware CRC instruction on Intel SSE 4.2 processors. This computes a
51 CRC-32C, *not* the CRC-32 used by Ethernet and zip, gzip, etc. A software
52 version is provided as a fall-back, as well as for speed comparisons. */
53
54/* Version history:
55 1.0 10 Feb 2013 First version
56 1.1 1 Aug 2013 Correct comments on why three crc instructions in parallel
57 */
58
59#include <stdio.h>
60#include <stdlib.h>
61#include <stdint.h>
62#include <unistd.h>
63#include <pthread.h>
64
65/* CRC-32C (iSCSI) polynomial in reversed bit order. */
66#define POLY 0x82f63b78
67
68/* Table for a quadword-at-a-time software crc. */
69static pthread_once_t crc32c_once_sw = PTHREAD_ONCE_INIT;
70static uint32_t crc32c_table[8][256];
71
72/* Construct table for software CRC-32C calculation. */
73static void crc32c_init_sw(void)
74{
75 uint32_t n, crc, k;
76
77 for (n = 0; n < 256; n++) {
78 crc = n;
79 crc = crc & 1 ? (crc >> 1) ^ POLY : crc >> 1;
80 crc = crc & 1 ? (crc >> 1) ^ POLY : crc >> 1;
81 crc = crc & 1 ? (crc >> 1) ^ POLY : crc >> 1;
82 crc = crc & 1 ? (crc >> 1) ^ POLY : crc >> 1;
83 crc = crc & 1 ? (crc >> 1) ^ POLY : crc >> 1;
84 crc = crc & 1 ? (crc >> 1) ^ POLY : crc >> 1;
85 crc = crc & 1 ? (crc >> 1) ^ POLY : crc >> 1;
86 crc = crc & 1 ? (crc >> 1) ^ POLY : crc >> 1;
87 crc32c_table[0][n] = crc;
88 }
89 for (n = 0; n < 256; n++) {
90 crc = crc32c_table[0][n];
91 for (k = 1; k < 8; k++) {
92 crc = crc32c_table[0][crc & 0xff] ^ (crc >> 8);
93 crc32c_table[k][n] = crc;
94 }
95 }
96}
97
98/* Table-driven software version as a fall-back. This is about 15 times slower
99 than using the hardware instructions. This assumes little-endian integers,
100 as is the case on Intel processors that the assembler code here is for. */
101uint32_t crc32c_sw(uint32_t crci, const void *buf, size_t len)
102{
103 const unsigned char *next = (const unsigned char*)buf;
104 uint64_t crc;
105
106 //printf("crc32c_sw!\n");
107
108 pthread_once(&crc32c_once_sw, crc32c_init_sw);
109 crc = crci ^ 0xffffffff;
110 while (len && ((uintptr_t)next & 7) != 0) {
111 crc = crc32c_table[0][(crc ^ *next++) & 0xff] ^ (crc >> 8);
112 len--;
113 }
114 while (len >= 8) {
115 crc ^= *(uint64_t *)next;
116 crc = crc32c_table[7][crc & 0xff] ^
117 crc32c_table[6][(crc >> 8) & 0xff] ^
118 crc32c_table[5][(crc >> 16) & 0xff] ^
119 crc32c_table[4][(crc >> 24) & 0xff] ^
120 crc32c_table[3][(crc >> 32) & 0xff] ^
121 crc32c_table[2][(crc >> 40) & 0xff] ^
122 crc32c_table[1][(crc >> 48) & 0xff] ^
123 crc32c_table[0][crc >> 56];
124 next += 8;
125 len -= 8;
126 }
127 while (len) {
128 crc = crc32c_table[0][(crc ^ *next++) & 0xff] ^ (crc >> 8);
129 len--;
130 }
131 return (uint32_t)crc ^ 0xffffffff;
132}
133
134/* Multiply a matrix times a vector over the Galois field of two elements,
135 GF(2). Each element is a bit in an unsigned integer. mat must have at
136 least as many entries as the power of two for most significant one bit in
137 vec. */
138static inline uint32_t gf2_matrix_times(uint32_t *mat, uint32_t vec)
139{
140 uint32_t sum;
141
142 sum = 0;
143 while (vec) {
144 if (vec & 1)
145 sum ^= *mat;
146 vec >>= 1;
147 mat++;
148 }
149 return sum;
150}
151
152/* Multiply a matrix by itself over GF(2). Both mat and square must have 32
153 rows. */
154static inline void gf2_matrix_square(uint32_t *square, uint32_t *mat)
155{
156 int n;
157
158 for (n = 0; n < 32; n++)
159 square[n] = gf2_matrix_times(mat, mat[n]);
160}
161
162#ifdef HAVE_AMD64_CRC32C
163
164/* Construct an operator to apply len zeros to a crc. len must be a power of
165 two. If len is not a power of two, then the result is the same as for the
166 largest power of two less than len. The result for len == 0 is the same as
167 for len == 1. A version of this routine could be easily written for any
168 len, but that is not needed for this application. */
169static void crc32c_zeros_op(uint32_t *even, size_t len)
170{
171 int n;
172 uint32_t row;
173 uint32_t odd[32]; /* odd-power-of-two zeros operator */
174
175 /* put operator for one zero bit in odd */
176 odd[0] = POLY; /* CRC-32C polynomial */
177 row = 1;
178 for (n = 1; n < 32; n++) {
179 odd[n] = row;
180 row <<= 1;
181 }
182
183 /* put operator for two zero bits in even */
184 gf2_matrix_square(even, odd);
185
186 /* put operator for four zero bits in odd */
187 gf2_matrix_square(odd, even);
188
189 /* first square will put the operator for one zero byte (eight zero bits),
190 in even -- next square puts operator for two zero bytes in odd, and so
191 on, until len has been rotated down to zero */
192 do {
193 gf2_matrix_square(even, odd);
194 len >>= 1;
195 if (len == 0)
196 return;
197 gf2_matrix_square(odd, even);
198 len >>= 1;
199 } while (len);
200
201 /* answer ended up in odd -- copy to even */
202 for (n = 0; n < 32; n++)
203 even[n] = odd[n];
204}
205
206/* Take a length and build four lookup tables for applying the zeros operator
207 for that length, byte-by-byte on the operand. */
208static void crc32c_zeros(uint32_t zeros[][256], size_t len)
209{
210 uint32_t n;
211 uint32_t op[32];
212
213 crc32c_zeros_op(op, len);
214 for (n = 0; n < 256; n++) {
215 zeros[0][n] = gf2_matrix_times(op, n);
216 zeros[1][n] = gf2_matrix_times(op, n << 8);
217 zeros[2][n] = gf2_matrix_times(op, n << 16);
218 zeros[3][n] = gf2_matrix_times(op, n << 24);
219 }
220}
221
222#endif
223
224/* Apply the zeros operator table to crc. */
225static inline uint32_t crc32c_shift(uint32_t zeros[][256], uint32_t crc)
226{
227 return zeros[0][crc & 0xff] ^ zeros[1][(crc >> 8) & 0xff] ^
228 zeros[2][(crc >> 16) & 0xff] ^ zeros[3][crc >> 24];
229}
230
231/* Block sizes for three-way parallel crc computation. LONG and SHORT must
232 both be powers of two. The associated string constants must be set
233 accordingly, for use in constructing the assembler instructions. */
234#define LONG 8192
235#define LONGx1 "8192"
236#define LONGx2 "16384"
237#define SHORT 256
238#define SHORTx1 "256"
239#define SHORTx2 "512"
240
241#ifdef HAVE_AMD64_CRC32C
242
243/* Tables for hardware crc that shift a crc by LONG and SHORT zeros. */
244static pthread_once_t crc32c_once_hw_amd64 = PTHREAD_ONCE_INIT;
245static uint32_t crc32c_long[4][256];
246static uint32_t crc32c_short[4][256];
247
248/* Initialize tables for shifting crcs. */
249static void crc32c_init_hw_amd64(void)
250{
251 crc32c_zeros(crc32c_long, LONG);
252 crc32c_zeros(crc32c_short, SHORT);
253}
254
255/* Compute CRC-32C using the Intel hardware instruction. */
256uint32_t crc32c_hw_amd64(uint32_t crc, const void *buf, size_t len)
257{
258 const unsigned char *next = (const unsigned char*)buf;
259 const unsigned char *end;
260 uint64_t crc0, crc1, crc2; /* need to be 64 bits for crc32q */
261
262 //printf("crc32c_hw_amd64!\n");
263
264 /* populate shift tables the first time through */
265 pthread_once(&crc32c_once_hw_amd64, crc32c_init_hw_amd64);
266
267 /* pre-process the crc */
268 crc0 = crc ^ 0xffffffff;
269
270 /* compute the crc for up to seven leading bytes to bring the data pointer
271 to an eight-byte boundary */
272 while (len && ((uintptr_t)next & 7) != 0) {
273 __asm__("crc32b\t" "(%1), %0"
274 : "=r"(crc0)
275 : "r"(next), "0"(crc0));
276 next++;
277 len--;
278 }
279
280 /* compute the crc on sets of LONG*3 bytes, executing three independent crc
281 instructions, each on LONG bytes -- this is optimized for the Nehalem,
282 Westmere, Sandy Bridge, and Ivy Bridge architectures, which have a
283 throughput of one crc per cycle, but a latency of three cycles */
284 while (len >= LONG*3) {
285 crc1 = 0;
286 crc2 = 0;
287 end = next + LONG;
288 do {
289 __asm__("crc32q\t" "(%3), %0\n\t"
290 "crc32q\t" LONGx1 "(%3), %1\n\t"
291 "crc32q\t" LONGx2 "(%3), %2"
292 : "=r"(crc0), "=r"(crc1), "=r"(crc2)
293 : "r"(next), "0"(crc0), "1"(crc1), "2"(crc2));
294 next += 8;
295 } while (next < end);
296 crc0 = crc32c_shift(crc32c_long, crc0) ^ crc1;
297 crc0 = crc32c_shift(crc32c_long, crc0) ^ crc2;
298 next += LONG*2;
299 len -= LONG*3;
300 }
301
302 /* do the same thing, but now on SHORT*3 blocks for the remaining data less
303 than a LONG*3 block */
304 while (len >= SHORT*3) {
305 crc1 = 0;
306 crc2 = 0;
307 end = next + SHORT;
308 do {
309 __asm__("crc32q\t" "(%3), %0\n\t"
310 "crc32q\t" SHORTx1 "(%3), %1\n\t"
311 "crc32q\t" SHORTx2 "(%3), %2"
312 : "=r"(crc0), "=r"(crc1), "=r"(crc2)
313 : "r"(next), "0"(crc0), "1"(crc1), "2"(crc2));
314 next += 8;
315 } while (next < end);
316 crc0 = crc32c_shift(crc32c_short, crc0) ^ crc1;
317 crc0 = crc32c_shift(crc32c_short, crc0) ^ crc2;
318 next += SHORT*2;
319 len -= SHORT*3;
320 }
321
322 /* compute the crc on the remaining eight-byte units less than a SHORT*3
323 block */
324 end = next + (len - (len & 7));
325 while (next < end) {
326 __asm__("crc32q\t" "(%1), %0"
327 : "=r"(crc0)
328 : "r"(next), "0"(crc0));
329 next += 8;
330 }
331 len &= 7;
332
333 /* compute the crc for up to seven trailing bytes */
334 while (len) {
335 __asm__("crc32b\t" "(%1), %0"
336 : "=r"(crc0)
337 : "r"(next), "0"(crc0));
338 next++;
339 len--;
340 }
341
342 /* return a post-processed crc */
343 return (uint32_t)crc0 ^ 0xffffffff;
344}
345
346/* Check for SSE 4.2. SSE 4.2 was first supported in Nehalem processors
347 introduced in November, 2008. This does not check for the existence of the
348 cpuid instruction itself, which was introduced on the 486SL in 1992, so this
349 will fail on earlier x86 processors. cpuid works on all Pentium and later
350 processors. */
351#define SSE42(have) \
352 do { \
353 uint32_t eax, ecx; \
354 eax = 1; \
355 __asm__("cpuid" \
356 : "=c"(ecx) \
357 : "a"(eax) \
358 : "%ebx", "%edx"); \
359 (have) = (ecx >> 20) & 1; \
360 } while (0)
361
362#endif // HAVE_AMD64_CRC32C
363
364#ifdef HAVE_ARM64_CRC32C
365
366uint32_t crc32c_hw_arm64(uint32_t crc, const void *buf, size_t len)
367{
368 crc = ~crc;
369 uint8_t *pd = (uint8_t *)buf;
370
371 //printf("crc32c_hw_arm64!\n");
372
373 // Align data if it's not aligned
374 while (((uintptr_t)pd & 7) && len > 0) {
375 crc = __crc32cb(crc, *(uint8_t *)pd);
376 pd++;
377 len--;
378 }
379
380 while (len >= 8) {
381 crc = __crc32cd(crc, *(uint64_t *)pd);
382 pd += 8;
383 len -= 8;
384 }
385
386 while (len > 0) {
387 crc = __crc32cb(crc, *(uint8_t *)pd);
388 pd++;
389 len--;
390 }
391
392 return ~crc;
393}
394
395#endif
396
397/* Compute a CRC-32C. If the crc32 instruction is available, use the hardware
398 version. Otherwise, use the software version. */
399uint32_t crc32c(uint32_t crc, const void *buf, size_t len)
400{
401#if defined(HAVE_AMD64_CRC32C)
402 int sse42;
403 SSE42(sse42);
404 return sse42 ? crc32c_hw_amd64(crc, buf, len) : crc32c_sw(crc, buf, len);
405#elif defined(HAVE_ARM64_CRC32C)
406 return crc32c_hw_arm64(crc, buf, len);
407#else
408#warning Hardware accelerated CRC32C is not available.
409 return crc32c_sw(crc, buf, len);
410#endif // HAVE_AMD64_CRC32C
411}
412
413uint32_t crc32c_hw(uint32_t crc, const void *buf, size_t len)
414{
415#if defined(HAVE_AMD64_CRC32C)
416 return crc32c_hw_amd64(crc, buf, len);
417#elif defined(HAVE_ARM64_CRC32C)
418 return crc32c_hw_arm64(crc, buf, len);
419#else
420#warning Hardware accelerated CRC32C is not available.
421 abort();
422#endif // HAVE_AMD64_CRC32C
423}
424
425#ifdef TEST
426
427#define SIZE (262144*3)
428#define CHUNK SIZE
429
430int main(int argc, char **argv)
431{
432 char *buf;
433 ssize_t got;
434 size_t off, n;
435 uint32_t crc;
436
437 (void)argv;
438 crc = 0;
439 buf = (char*)malloc(SIZE);
440 if (buf == NULL) {
441 fputs("out of memory", stderr);
442 return 1;
443 }
444 while ((got = read(0, buf, SIZE)) > 0) {
445 off = 0;
446 do {
447 n = (size_t)got - off;
448 if (n > CHUNK)
449 n = CHUNK;
450 crc = argc > 1 ? crc32c_sw(crc, buf + off, n) :
451 crc32c(crc, buf + off, n);
452 off += n;
453 } while (off < (size_t)got);
454 }
455 free(buf);
456 if (got == -1) {
457 fputs("read error\n", stderr);
458 return 1;
459 }
460 printf("%08x\n", crc);
461 return 0;
462}
463
464#endif /* TEST */
465
466/* emacs
467 * Local Variables:
468 * tab-width: 8
469 * c-basic-offset: 4
470 * indent-tabs-mode: nil
471 * End:
472 */
#define LONGx2
Definition crc32c.cxx:236
#define SHORTx1
Definition crc32c.cxx:238
static uint32_t gf2_matrix_times(uint32_t *mat, uint32_t vec)
Definition crc32c.cxx:138
#define LONGx1
Definition crc32c.cxx:235
uint32_t crc32c(uint32_t crc, const void *buf, size_t len)
Definition crc32c.cxx:399
static uint32_t crc32c_table[8][256]
Definition crc32c.cxx:70
static pthread_once_t crc32c_once_sw
Definition crc32c.cxx:69
#define SHORTx2
Definition crc32c.cxx:239
static uint32_t crc32c_shift(uint32_t zeros[][256], uint32_t crc)
Definition crc32c.cxx:225
static void crc32c_init_sw(void)
Definition crc32c.cxx:73
static void gf2_matrix_square(uint32_t *square, uint32_t *mat)
Definition crc32c.cxx:154
uint32_t crc32c_hw(uint32_t crc, const void *buf, size_t len)
Definition crc32c.cxx:413
#define SHORT
Definition crc32c.cxx:237
#define POLY
Definition crc32c.cxx:66
#define LONG
Definition crc32c.cxx:234
uint32_t crc32c_sw(uint32_t crci, const void *buf, size_t len)
Definition crc32c.cxx:101
#define CHUNK
#define SIZE
int main()
Definition hwtest.cxx:23
DWORD n[4]
Definition mana.cxx:247
#define end
#define read(n, a, f)
INT k
Definition odbhist.cxx:40