3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 only,
7 * as published by the Free Software Foundation.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License version 2 for more details (a copy is included
13 * in the LICENSE file that accompanied this code).
15 * You should have received a copy of the GNU General Public License
16 * version 2 along with this program; If not, see http://www.gnu.org/licenses
18 * Please visit http://www.xyratex.com/contact if you need additional
19 * information or have any questions.
25 * Copyright 2012 Xyratex Technology Limited
27 * Using hardware provided PCLMULQDQ instruction to accelerate the CRC32
29 * CRC32 polynomial:0x04c11db7(BE)/0xEDB88320(LE)
30 * PCLMULQDQ is a new instruction in Intel SSE4.2, the reference can be found
32 * http://www.intel.com/products/processor/manuals/
33 * Intel(R) 64 and IA-32 Architectures Software Developer's Manual
34 * Volume 2B: Instruction Set Reference, N-Z
36 * Authors: Gregory Prestas <Gregory_Prestas@us.xyratex.com>
37 * Alexander Boyko <Alexander_Boyko@xyratex.com>
40 /* gcc 4.1.2 does not support pclmulqdq instruction
41 * Use macro defenition from linux kernel 2.6.38 */
43 #define REG_NUM_INVALID 100
44 .macro XMM_NUM opd xmm
45 \opd = REG_NUM_INVALID
100 .macro PFX_REX opd1 opd2 W=0
101 .if ((\opd1 | \opd2) & 8) || \W
102 .byte 0x40 | ((\opd1 & 8) >> 3) | ((\opd2 & 8) >> 1) | (\W << 3)
106 .macro MODRM mod opd1 opd2
107 .byte \mod | (\opd1 & 7) | ((\opd2 & 7) << 3)
110 .macro PCLMULQDQ imm8 xmm1 xmm2
111 XMM_NUM clmul_opd1 \xmm1
112 XMM_NUM clmul_opd2 \xmm2
114 PFX_REX clmul_opd1 clmul_opd2
115 .byte 0x0f, 0x3a, 0x44
116 MODRM 0xc0 clmul_opd1 clmul_opd2
123 * [x4*128+32 mod P(x) << 32)]' << 1 = 0x154442bd4
124 * #define CONSTANT_R1 0x154442bd4LL
126 * [(x4*128-32 mod P(x) << 32)]' << 1 = 0x1c6e41596
127 * #define CONSTANT_R2 0x1c6e41596LL
130 .octa 0x00000001c6e415960000000154442bd4
132 * [(x128+32 mod P(x) << 32)]' << 1 = 0x1751997d0
133 * #define CONSTANT_R3 0x1751997d0LL
135 * [(x128-32 mod P(x) << 32)]' << 1 = 0x0ccaa009e
136 * #define CONSTANT_R4 0x0ccaa009eLL
139 .octa 0x00000000ccaa009e00000001751997d0
141 * [(x64 mod P(x) << 32)]' << 1 = 0x163cd6124
142 * #define CONSTANT_R5 0x163cd6124LL
145 .octa 0x00000000000000000000000163cd6124
147 .octa 0x000000000000000000000000FFFFFFFF
149 * #define CRCPOLY_TRUE_LE_FULL 0x1DB710641LL
151 * Barrett Reduction constant (u64`) = u` = (x**64 / P(x))` = 0x1F7011641LL
152 * #define CONSTANT_RU 0x1F7011641LL
155 .octa 0x00000001F701164100000001DB710641
157 #define CONSTANT %xmm0
174 * BUF - buffer (16 bytes aligned)
175 * LEN - sizeof buffer (16 bytes aligned), LEN should be grater than 63
176 * CRC - initial crc32
178 * uint crc32_pclmul_le_16(unsigned char const *buffer,
179 * size_t len, uint crc32)
181 .globl crc32_pclmul_le_16
183 crc32_pclmul_le_16:/* buffer and buffer size are 16 bytes aligned */
185 movdqa 0x10(BUF), %xmm2
186 movdqa 0x20(BUF), %xmm3
187 movdqa 0x30(BUF), %xmm4
193 /* This is for position independed code(-fPIC) support for 32bit */
202 movdqa .Lconstant_R2R1(%rip), CONSTANT
204 movdqa .Lconstant_R2R1 - delta(%ecx), CONSTANT
207 loop_64:/* 64 bytes Full cache line folding */
208 prefetchnta 0x40(BUF)
215 PCLMULQDQ 00, CONSTANT, %xmm1
216 PCLMULQDQ 00, CONSTANT, %xmm2
217 PCLMULQDQ 00, CONSTANT, %xmm3
219 PCLMULQDQ 00, CONSTANT, %xmm4
221 PCLMULQDQ 0x11, CONSTANT, %xmm5
222 PCLMULQDQ 0x11, CONSTANT, %xmm6
223 PCLMULQDQ 0x11, CONSTANT, %xmm7
225 PCLMULQDQ 0x11, CONSTANT, %xmm8
233 /* xmm8 unsupported for x32 */
235 PCLMULQDQ 00, CONSTANT, %xmm4
236 PCLMULQDQ 0x11, CONSTANT, %xmm5
241 pxor 0x10(BUF), %xmm2
242 pxor 0x20(BUF), %xmm3
243 pxor 0x30(BUF), %xmm4
249 less_64:/* Folding cache line into 128bit */
251 movdqa .Lconstant_R4R3(%rip), CONSTANT
253 movdqa .Lconstant_R4R3 - delta(%ecx), CONSTANT
258 PCLMULQDQ 0x00, CONSTANT, %xmm1
259 PCLMULQDQ 0x11, CONSTANT, %xmm5
264 PCLMULQDQ 0x00, CONSTANT, %xmm1
265 PCLMULQDQ 0x11, CONSTANT, %xmm5
270 PCLMULQDQ 0x00, CONSTANT, %xmm1
271 PCLMULQDQ 0x11, CONSTANT, %xmm5
277 loop_16:/* Folding rest buffer into 128bit */
279 PCLMULQDQ 0x00, CONSTANT, %xmm1
280 PCLMULQDQ 0x11, CONSTANT, %xmm5
289 /* perform the last 64 bit fold, also adds 32 zeroes
290 * to the input stream */
291 PCLMULQDQ 0x01, %xmm1, CONSTANT /* R4 * xmm1.low */
295 /* final 32-bit fold */
298 movdqa .Lconstant_R5(%rip), CONSTANT
299 movdqa .Lconstant_mask32(%rip), %xmm3
301 movdqa .Lconstant_R5 - delta(%ecx), CONSTANT
302 movdqa .Lconstant_mask32 - delta(%ecx), %xmm3
306 PCLMULQDQ 0x00, CONSTANT, %xmm1
309 /* Finish up with the bit-reversed barrett reduction 64 ==> 32 bits */
311 movdqa .Lconstant_RUpoly(%rip), CONSTANT
313 movdqa .Lconstant_RUpoly - delta(%ecx), CONSTANT
317 PCLMULQDQ 0x10, CONSTANT, %xmm1
319 PCLMULQDQ 0x00, CONSTANT, %xmm1
321 pextrd $0x01, %xmm1, %eax