blob: 436479b103d1cd8ee1fd886e749bb3d02e38886c [file] [log] [blame]
James Kuszmaul64391362021-01-17 11:32:00 -08001/*
2 * crc32c.c -- compute CRC-32C using the Intel crc32 instruction
3 * Copyright (C) 2013 Mark Adler
4 * Version 1.1 1 Aug 2013 Mark Adler
5 *
6 * Use hardware CRC instruction on Intel SSE 4.2 processors. This computes a
7 * CRC-32C, *not* the CRC-32 used by Ethernet and zip, gzip, etc. A software
8 * version is provided as a fall-back, as well as for speed comparisons.
9 *
10 * (Modified for the use in RAWRTC. Software fallback has been stripped.)
11 *
12 *
13 *
14 * This software is provided 'as-is', without any express or implied
15 * warranty. In no event will the author be held liable for any damages
16 * arising from the use of this software.
17 *
18 * Permission is granted to anyone to use this software for any purpose,
19 * including commercial applications, and to alter it and redistribute it
20 * freely, subject to the following restrictions:
21 *
22 * 1. The origin of this software must not be misrepresented; you must not
23 * claim that you wrote the original software. If you use this software
24 * in a product, an acknowledgment in the product documentation would be
25 * appreciated but is not required.
26 * 2. Altered source versions must be plainly marked as such, and must not be
27 * misrepresented as being the original software.
28 * 3. This notice may not be removed or altered from any source distribution.
29 *
30 * Mark Adler
31 * madler@alumni.caltech.edu
32 */
33#include "sse42.h"
34#include <re.h>
35
36/* CRC-32C (iSCSI) polynomial in reversed bit order. */
37#define POLY 0x82f63b78
38
39/* Multiply a matrix times a vector over the Galois field of two elements,
40 GF(2). Each element is a bit in an unsigned integer. mat must have at
41 least as many entries as the power of two for most significant one bit in
42 vec. */
43static inline uint32_t gf2_matrix_times(uint32_t* mat, uint32_t vec) {
44 uint32_t sum;
45
46 sum = 0;
47 while (vec) {
48 if (vec & 1) {
49 sum ^= *mat;
50 }
51 vec >>= 1;
52 mat++;
53 }
54 return sum;
55}
56
57/* Multiply a matrix by itself over GF(2). Both mat and square must have 32
58 rows. */
59static inline void gf2_matrix_square(uint32_t* square, uint32_t* mat) {
60 int n;
61
62 for (n = 0; n < 32; n++) {
63 square[n] = gf2_matrix_times(mat, mat[n]);
64 }
65}
66
67/* Construct an operator to apply len zeros to a crc. len must be a power of
68 two. If len is not a power of two, then the result is the same as for the
69 largest power of two less than len. The result for len == 0 is the same as
70 for len == 1. A version of this routine could be easily written for any
71 len, but that is not needed for this application. */
72static void crc32c_zeros_op(uint32_t* even, size_t len) {
73 int n;
74 uint32_t row;
75 uint32_t odd[32]; /* odd-power-of-two zeros operator */
76
77 /* put operator for one zero bit in odd */
78 odd[0] = POLY; /* CRC-32C polynomial */
79 row = 1;
80 for (n = 1; n < 32; n++) {
81 odd[n] = row;
82 row <<= 1;
83 }
84
85 /* put operator for two zero bits in even */
86 gf2_matrix_square(even, odd);
87
88 /* put operator for four zero bits in odd */
89 gf2_matrix_square(odd, even);
90
91 /* first square will put the operator for one zero byte (eight zero bits),
92 in even -- next square puts operator for two zero bytes in odd, and so
93 on, until len has been rotated down to zero */
94 do {
95 gf2_matrix_square(even, odd);
96 len >>= 1;
97 if (len == 0) {
98 return;
99 }
100 gf2_matrix_square(odd, even);
101 len >>= 1;
102 } while (len);
103
104 /* answer ended up in odd -- copy to even */
105 for (n = 0; n < 32; n++) {
106 even[n] = odd[n];
107 }
108}
109
110/* Take a length and build four lookup tables for applying the zeros operator
111 for that length, byte-by-byte on the operand. */
112static void crc32c_zeros(uint32_t zeros[][256], size_t len) {
113 uint32_t n;
114 uint32_t op[32];
115
116 crc32c_zeros_op(op, len);
117 for (n = 0; n < 256; n++) {
118 zeros[0][n] = gf2_matrix_times(op, n);
119 zeros[1][n] = gf2_matrix_times(op, n << 8);
120 zeros[2][n] = gf2_matrix_times(op, n << 16);
121 zeros[3][n] = gf2_matrix_times(op, n << 24);
122 }
123}
124
125/* Apply the zeros operator table to crc. */
126static inline uint32_t crc32c_shift(uint32_t zeros[][256], uint32_t crc) {
127 // clang-format off
128 return zeros[0][crc & 0xff] ^ zeros[1][(crc >> 8) & 0xff] ^
129 zeros[2][(crc >> 16) & 0xff] ^ zeros[3][crc >> 24];
130 // clang-format on
131}
132
133/* Block sizes for three-way parallel crc computation. LONG and SHORT must
134 both be powers of two. The associated string constants must be set
135 accordingly, for use in constructing the assembler instructions. */
136#define LONG 8192
137#define LONGx1 "8192"
138#define LONGx2 "16384"
139#define SHORT 256
140#define SHORTx1 "256"
141#define SHORTx2 "512"
142
143/* Tables for hardware crc that shift a crc by LONG and SHORT zeros. */
144static uint32_t crc32c_long[4][256];
145static uint32_t crc32c_short[4][256];
146
147/* Compute CRC-32C using the Intel hardware instruction. */
148static uint32_t crc32c_hw(uint32_t crc, const void* buf, size_t len) {
149 const unsigned char* next = buf;
150 const unsigned char* end;
151 uint64_t crc0, crc1, crc2; /* need to be 64 bits for crc32q */
152
153 /* pre-process the crc */
154 crc0 = crc ^ 0xffffffff;
155
156 /* compute the crc for up to seven leading bytes to bring the data pointer
157 to an eight-byte boundary */
158 while (len && ((uintptr_t) next & 7) != 0) {
159 __asm__("crc32b\t"
160 "(%1), %0"
161 : "=r"(crc0)
162 : "r"(next), "0"(crc0));
163 next++;
164 len--;
165 }
166
167 /* compute the crc on sets of LONG*3 bytes, executing three independent crc
168 instructions, each on LONG bytes -- this is optimized for the Nehalem,
169 Westmere, Sandy Bridge, and Ivy Bridge architectures, which have a
170 throughput of one crc per cycle, but a latency of three cycles */
171 while (len >= LONG * 3) {
172 crc1 = 0;
173 crc2 = 0;
174 end = next + LONG;
175 do {
176 __asm__("crc32q\t"
177 "(%3), %0\n\t"
178 "crc32q\t" LONGx1 "(%3), %1\n\t"
179 "crc32q\t" LONGx2 "(%3), %2"
180 : "=r"(crc0), "=r"(crc1), "=r"(crc2)
181 : "r"(next), "0"(crc0), "1"(crc1), "2"(crc2));
182 next += 8;
183 } while (next < end);
184 crc0 = crc32c_shift(crc32c_long, (uint32_t) crc0) ^ crc1;
185 crc0 = crc32c_shift(crc32c_long, (uint32_t) crc0) ^ crc2;
186 next += LONG * 2;
187 len -= LONG * 3;
188 }
189
190 /* do the same thing, but now on SHORT*3 blocks for the remaining data less
191 than a LONG*3 block */
192 while (len >= SHORT * 3) {
193 crc1 = 0;
194 crc2 = 0;
195 end = next + SHORT;
196 do {
197 __asm__("crc32q\t"
198 "(%3), %0\n\t"
199 "crc32q\t" SHORTx1 "(%3), %1\n\t"
200 "crc32q\t" SHORTx2 "(%3), %2"
201 : "=r"(crc0), "=r"(crc1), "=r"(crc2)
202 : "r"(next), "0"(crc0), "1"(crc1), "2"(crc2));
203 next += 8;
204 } while (next < end);
205 crc0 = crc32c_shift(crc32c_short, (uint32_t) crc0) ^ crc1;
206 crc0 = crc32c_shift(crc32c_short, (uint32_t) crc0) ^ crc2;
207 next += SHORT * 2;
208 len -= SHORT * 3;
209 }
210
211 /* compute the crc on the remaining eight-byte units less than a SHORT*3
212 block */
213 end = next + (len - (len & 7));
214 while (next < end) {
215 __asm__("crc32q\t"
216 "(%1), %0"
217 : "=r"(crc0)
218 : "r"(next), "0"(crc0));
219 next += 8;
220 }
221 len &= 7;
222
223 /* compute the crc for up to seven trailing bytes */
224 while (len) {
225 __asm__("crc32b\t"
226 "(%1), %0"
227 : "=r"(crc0)
228 : "r"(next), "0"(crc0));
229 next++;
230 len--;
231 }
232
233 /* return a post-processed crc */
234 return (uint32_t) crc0 ^ 0xffffffff;
235}
236
237/*
238 * Probe for SSE 4.2 support.
239 */
240bool rawrtc_crc32c_sse42_supported(void) {
241 uint32_t eax, ecx;
242 eax = 1;
243 __asm__("cpuid" : "=c"(ecx) : "a"(eax) : "%ebx", "%edx");
244 return ((ecx >> 20) & 1) != 0;
245}
246
247/*
248 * Initialize tables for shifting CRCs.
249 */
250void rawrtc_crc32c_init_sse42(void) {
251 crc32c_zeros(crc32c_long, LONG);
252 crc32c_zeros(crc32c_short, SHORT);
253}
254
255/*
256 * Compute CRC-32C using hardware instructions.
257 */
258uint32_t rawrtc_crc32c_sse42(void const* buffer, size_t length) {
259 return crc32c_hw(0x00000000, buffer, length);
260}