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