| 1 |
/* crc-intel-pclmul.c - Intel PCLMUL accelerated CRC implementation |
| 2 |
* Copyright (C) 2016 Jussi Kivilinna <[email protected]> |
| 3 |
* |
| 4 |
* This file is part of Libgcrypt. |
| 5 |
* |
| 6 |
* Libgcrypt is free software; you can redistribute it and/or modify |
| 7 |
* it under the terms of the GNU Lesser General Public License as |
| 8 |
* published by the Free Software Foundation; either version 2.1 of |
| 9 |
* the License, or (at your option) any later version. |
| 10 |
* |
| 11 |
* Libgcrypt is distributed in the hope that it will be useful, |
| 12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 |
* GNU Lesser General Public License for more details. |
| 15 |
* |
| 16 |
* You should have received a copy of the GNU Lesser General Public |
| 17 |
* License along with this program; if not, see <https://www.gnu.org/licenses/>. |
| 18 |
* SPDX-License-Identifier: LGPL-2.1-or-later |
| 19 |
* |
| 20 |
*/ |
| 21 |
|
| 22 |
#include <config.h> |
| 23 |
#include <stdio.h> |
| 24 |
#include <stdlib.h> |
| 25 |
#include <string.h> |
| 26 |
|
| 27 |
#include "g10lib.h" |
| 28 |
|
| 29 |
#include "bithelp.h" |
| 30 |
#include "bufhelp.h" |
| 31 |
|
| 32 |
|
| 33 |
#if defined(ENABLE_PCLMUL_SUPPORT) && defined(ENABLE_SSE41_SUPPORT) && \ |
| 34 |
__GNUC__ >= 4 && \ |
| 35 |
((defined(__i386__) && SIZEOF_UNSIGNED_LONG == 4) || defined(__x86_64__)) |
| 36 |
|
| 37 |
|
| 38 |
#if _GCRY_GCC_VERSION >= 40400 /* 4.4 */ |
| 39 |
/* Prevent compiler from issuing SSE instructions between asm blocks. */ |
| 40 |
# pragma GCC target("no-sse") |
| 41 |
#endif |
| 42 |
#if __clang__ |
| 43 |
# pragma clang attribute push (__attribute__((target("no-sse"))), apply_to = function) |
| 44 |
#endif |
| 45 |
|
| 46 |
|
| 47 |
#define ALWAYS_INLINE inline __attribute__((always_inline)) |
| 48 |
#define NO_INSTRUMENT_FUNCTION __attribute__((no_instrument_function)) |
| 49 |
|
| 50 |
#define ASM_FUNC_ATTR NO_INSTRUMENT_FUNCTION |
| 51 |
#define ASM_FUNC_ATTR_INLINE ASM_FUNC_ATTR ALWAYS_INLINE |
| 52 |
|
| 53 |
|
| 54 |
#define ALIGNED_16 __attribute__ ((aligned (16))) |
| 55 |
|
| 56 |
|
| 57 |
struct u16_unaligned_s |
| 58 |
{ |
| 59 |
u16 a; |
| 60 |
} __attribute__((packed, aligned (1), may_alias)); |
| 61 |
|
| 62 |
|
| 63 |
/* Constants structure for generic reflected/non-reflected CRC32 CLMUL |
| 64 |
* functions. */ |
| 65 |
struct crc32_consts_s |
| 66 |
{ |
| 67 |
/* k: { x^(32*17), x^(32*15), x^(32*5), x^(32*3), x^(32*2), 0 } mod P(x) */ |
| 68 |
u64 k[6]; |
| 69 |
/* my_p: { floor(x^64 / P(x)), P(x) } */ |
| 70 |
u64 my_p[2]; |
| 71 |
/* k_ymm: { x^(32*33), x^(32*31) } mod P(x) */ |
| 72 |
u64 k_ymm[2]; |
| 73 |
/* k_zmm: { x^(32*65), x^(32*63) } mod P(x) */ |
| 74 |
u64 k_zmm[2]; |
| 75 |
}; |
| 76 |
|
| 77 |
|
| 78 |
/* CLMUL constants for CRC32 and CRC32RFC1510. */ |
| 79 |
static const struct crc32_consts_s crc32_consts ALIGNED_16 = |
| 80 |
{ |
| 81 |
{ /* k[6] = reverse_33bits( x^(32*y) mod P(x) ) */ |
| 82 |
U64_C(0x154442bd4), U64_C(0x1c6e41596), /* y = { 17, 15 } */ |
| 83 |
U64_C(0x1751997d0), U64_C(0x0ccaa009e), /* y = { 5, 3 } */ |
| 84 |
U64_C(0x163cd6124), 0 /* y = 2 */ |
| 85 |
}, |
| 86 |
{ /* my_p[2] = reverse_33bits ( { floor(x^64 / P(x)), P(x) } ) */ |
| 87 |
U64_C(0x1f7011641), U64_C(0x1db710641) |
| 88 |
}, |
| 89 |
{ /* k_ymm[2] */ |
| 90 |
U64_C(0x1e88ef372), U64_C(0x14a7fe880) /* y = { 33, 31 } */, |
| 91 |
}, |
| 92 |
{ /* k_zmm[2] */ |
| 93 |
U64_C(0x11542778a), U64_C(0x1322d1430) /* y = { 65, 63 } */ |
| 94 |
} |
| 95 |
}; |
| 96 |
|
| 97 |
/* CLMUL constants for CRC24RFC2440 (polynomial multiplied with x⁸). */ |
| 98 |
static const struct crc32_consts_s crc24rfc2440_consts ALIGNED_16 = |
| 99 |
{ |
| 100 |
{ /* k[6] = x^(32*y) mod P(x) << 32*/ |
| 101 |
U64_C(0x08289a00) << 32, U64_C(0x74b44a00) << 32, /* y = { 17, 15 } */ |
| 102 |
U64_C(0xc4b14d00) << 32, U64_C(0xfd7e0c00) << 32, /* y = { 5, 3 } */ |
| 103 |
U64_C(0xd9fe8c00) << 32, 0 /* y = 2 */ |
| 104 |
}, |
| 105 |
{ /* my_p[2] = { floor(x^64 / P(x)), P(x) } */ |
| 106 |
U64_C(0x1f845fe24), U64_C(0x1864cfb00) |
| 107 |
}, |
| 108 |
{ /* k_ymm[2] */ |
| 109 |
U64_C(0xaee5d500) << 32, U64_C(0x1a43ea00) << 32 /* y = { 33, 31 } */ |
| 110 |
}, |
| 111 |
{ /* k_zmm[2] */ |
| 112 |
U64_C(0x21342700) << 32, U64_C(0x5d2b6300) << 32 /* y = { 65, 63 } */ |
| 113 |
} |
| 114 |
}; |
| 115 |
|
| 116 |
/* Common constants for CRC32 algorithms. */ |
| 117 |
static const byte crc32_refl_shuf_shift[3 * 16] ALIGNED_16 = |
| 118 |
{ |
| 119 |
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 120 |
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 121 |
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, |
| 122 |
0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, |
| 123 |
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 124 |
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 125 |
}; |
| 126 |
static const byte crc32_shuf_shift[3 * 16] ALIGNED_16 = |
| 127 |
{ |
| 128 |
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 129 |
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 130 |
0x0f, 0x0e, 0x0d, 0x0c, 0x0b, 0x0a, 0x09, 0x08, |
| 131 |
0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01, 0x00, |
| 132 |
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 133 |
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 134 |
}; |
| 135 |
static const byte *crc32_bswap_shuf = &crc32_shuf_shift[16]; |
| 136 |
static const byte crc32_partial_fold_input_mask[16 + 16] ALIGNED_16 = |
| 137 |
{ |
| 138 |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 139 |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 140 |
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 141 |
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 142 |
}; |
| 143 |
static const u64 crc32_merge9to15_shuf[15 - 9 + 1][2] ALIGNED_16 = |
| 144 |
{ |
| 145 |
{ U64_C(0x0706050403020100), U64_C(0xffffffffffffff0f) }, /* 9 */ |
| 146 |
{ U64_C(0x0706050403020100), U64_C(0xffffffffffff0f0e) }, |
| 147 |
{ U64_C(0x0706050403020100), U64_C(0xffffffffff0f0e0d) }, |
| 148 |
{ U64_C(0x0706050403020100), U64_C(0xffffffff0f0e0d0c) }, |
| 149 |
{ U64_C(0x0706050403020100), U64_C(0xffffff0f0e0d0c0b) }, |
| 150 |
{ U64_C(0x0706050403020100), U64_C(0xffff0f0e0d0c0b0a) }, |
| 151 |
{ U64_C(0x0706050403020100), U64_C(0xff0f0e0d0c0b0a09) }, /* 15 */ |
| 152 |
}; |
| 153 |
static const u64 crc32_merge5to7_shuf[7 - 5 + 1][2] ALIGNED_16 = |
| 154 |
{ |
| 155 |
{ U64_C(0xffffff0703020100), U64_C(0xffffffffffffffff) }, /* 5 */ |
| 156 |
{ U64_C(0xffff070603020100), U64_C(0xffffffffffffffff) }, |
| 157 |
{ U64_C(0xff07060503020100), U64_C(0xffffffffffffffff) }, /* 7 */ |
| 158 |
}; |
| 159 |
|
| 160 |
/* PCLMUL functions for reflected CRC32. */ |
| 161 |
static ASM_FUNC_ATTR_INLINE void |
| 162 |
crc32_reflected_bulk (u32 *pcrc, const byte *inbuf, size_t inlen, |
| 163 |
const struct crc32_consts_s *consts, u32 hwfeatures) |
| 164 |
{ |
| 165 |
if (inlen >= 8 * 16) |
| 166 |
{ |
| 167 |
if ((hwfeatures & HWF_INTEL_VAES_VPCLMUL) |
| 168 |
&& (hwfeatures & HWF_INTEL_AVX2) |
| 169 |
&& inlen >= 8 * 32) |
| 170 |
{ |
| 171 |
if ((hwfeatures & HWF_INTEL_VAES_VPCLMUL) |
| 172 |
&& (hwfeatures & HWF_INTEL_AVX512) |
| 173 |
&& inlen >= 8 * 64) |
| 174 |
{ |
| 175 |
asm volatile("vmovd %[crc], %%xmm4\n\t" |
| 176 |
"vpopcntb %%xmm4, %%xmm0\n\t" /* spec stop for old AVX512 CPUs */ |
| 177 |
"vmovdqu64 %[inbuf_0], %%zmm0\n\t" |
| 178 |
"vmovdqu64 %[inbuf_1], %%zmm1\n\t" |
| 179 |
"vmovdqu64 %[inbuf_2], %%zmm2\n\t" |
| 180 |
"vmovdqu64 %[inbuf_3], %%zmm3\n\t" |
| 181 |
"vpxorq %%zmm4, %%zmm0, %%zmm0\n\t" |
| 182 |
: |
| 183 |
: [crc] "m" (*pcrc), |
| 184 |
[inbuf_0] "m" (inbuf[0 * 64]), |
| 185 |
[inbuf_1] "m" (inbuf[1 * 64]), |
| 186 |
[inbuf_2] "m" (inbuf[2 * 64]), |
| 187 |
[inbuf_3] "m" (inbuf[3 * 64]), |
| 188 |
[k_zmm] "m" (consts->k_zmm[0]) |
| 189 |
); |
| 190 |
|
| 191 |
inbuf += 4 * 64; |
| 192 |
inlen -= 4 * 64; |
| 193 |
|
| 194 |
asm volatile("vbroadcasti32x4 %[k_zmm], %%zmm4\n\t" |
| 195 |
: |
| 196 |
: [k_zmm] "m" (consts->k_zmm[0]) |
| 197 |
); |
| 198 |
|
| 199 |
/* Fold by 16. */ |
| 200 |
while (inlen >= 4 * 64) |
| 201 |
{ |
| 202 |
asm volatile ("vmovdqu64 %[inbuf_0], %%zmm5\n\t" |
| 203 |
"vmovdqa64 %%zmm0, %%zmm6\n\t" |
| 204 |
"vpclmulqdq $0x00, %%zmm4, %%zmm0, %%zmm0\n\t" |
| 205 |
"vpclmulqdq $0x11, %%zmm4, %%zmm6, %%zmm6\n\t" |
| 206 |
"vpternlogq $0x96, %%zmm5, %%zmm6, %%zmm0\n\t" |
| 207 |
|
| 208 |
"vmovdqu64 %[inbuf_1], %%zmm5\n\t" |
| 209 |
"vmovdqa64 %%zmm1, %%zmm6\n\t" |
| 210 |
"vpclmulqdq $0x00, %%zmm4, %%zmm1, %%zmm1\n\t" |
| 211 |
"vpclmulqdq $0x11, %%zmm4, %%zmm6, %%zmm6\n\t" |
| 212 |
"vpternlogq $0x96, %%zmm5, %%zmm6, %%zmm1\n\t" |
| 213 |
|
| 214 |
"vmovdqu64 %[inbuf_2], %%zmm5\n\t" |
| 215 |
"vmovdqa64 %%zmm2, %%zmm6\n\t" |
| 216 |
"vpclmulqdq $0x00, %%zmm4, %%zmm2, %%zmm2\n\t" |
| 217 |
"vpclmulqdq $0x11, %%zmm4, %%zmm6, %%zmm6\n\t" |
| 218 |
"vpternlogq $0x96, %%zmm5, %%zmm6, %%zmm2\n\t" |
| 219 |
|
| 220 |
"vmovdqu64 %[inbuf_3], %%zmm5\n\t" |
| 221 |
"vmovdqa64 %%zmm3, %%zmm6\n\t" |
| 222 |
"vpclmulqdq $0x00, %%zmm4, %%zmm3, %%zmm3\n\t" |
| 223 |
"vpclmulqdq $0x11, %%zmm4, %%zmm6, %%zmm6\n\t" |
| 224 |
"vpternlogq $0x96, %%zmm5, %%zmm6, %%zmm3\n\t" |
| 225 |
: |
| 226 |
: [inbuf_0] "m" (inbuf[0 * 64]), |
| 227 |
[inbuf_1] "m" (inbuf[1 * 64]), |
| 228 |
[inbuf_2] "m" (inbuf[2 * 64]), |
| 229 |
[inbuf_3] "m" (inbuf[3 * 64]) |
| 230 |
); |
| 231 |
|
| 232 |
inbuf += 4 * 64; |
| 233 |
inlen -= 4 * 64; |
| 234 |
} |
| 235 |
|
| 236 |
/* Fold 16 to 8. */ |
| 237 |
asm volatile("vbroadcasti32x4 %[k_ymm], %%zmm4\n\t" |
| 238 |
/* Fold zmm2 into zmm0. */ |
| 239 |
"vmovdqa64 %%zmm0, %%zmm5\n\t" |
| 240 |
"vpclmulqdq $0x00, %%zmm4, %%zmm5, %%zmm5\n\t" |
| 241 |
"vpclmulqdq $0x11, %%zmm4, %%zmm0, %%zmm0\n\t" |
| 242 |
"vpternlogq $0x96, %%zmm2, %%zmm5, %%zmm0\n\t" |
| 243 |
/* Fold zmm3 into zmm1. */ |
| 244 |
"vmovdqa64 %%zmm1, %%zmm5\n\t" |
| 245 |
"vpclmulqdq $0x00, %%zmm4, %%zmm5, %%zmm5\n\t" |
| 246 |
"vpclmulqdq $0x11, %%zmm4, %%zmm1, %%zmm1\n\t" |
| 247 |
"vpternlogq $0x96, %%zmm3, %%zmm5, %%zmm1\n\t" |
| 248 |
: |
| 249 |
: [k_ymm] "m" (consts->k_ymm[0])); |
| 250 |
|
| 251 |
asm volatile("vextracti64x4 $1, %%zmm1, %%ymm3\n\t" |
| 252 |
"vmovdqa %%ymm1, %%ymm2\n\t" |
| 253 |
"vextracti64x4 $1, %%zmm0, %%ymm1\n\t" |
| 254 |
: |
| 255 |
: ); |
| 256 |
} |
| 257 |
else |
| 258 |
{ |
| 259 |
asm volatile ("vmovd %[crc], %%xmm4\n\t" |
| 260 |
"vmovdqu %[inbuf_0], %%ymm0\n\t" |
| 261 |
"vmovdqu %[inbuf_1], %%ymm1\n\t" |
| 262 |
"vmovdqu %[inbuf_2], %%ymm2\n\t" |
| 263 |
"vmovdqu %[inbuf_3], %%ymm3\n\t" |
| 264 |
"vpxor %%ymm4, %%ymm0, %%ymm0\n\t" |
| 265 |
: |
| 266 |
: [inbuf_0] "m" (inbuf[0 * 32]), |
| 267 |
[inbuf_1] "m" (inbuf[1 * 32]), |
| 268 |
[inbuf_2] "m" (inbuf[2 * 32]), |
| 269 |
[inbuf_3] "m" (inbuf[3 * 32]), |
| 270 |
[crc] "m" (*pcrc) |
| 271 |
); |
| 272 |
|
| 273 |
inbuf += 4 * 32; |
| 274 |
inlen -= 4 * 32; |
| 275 |
|
| 276 |
asm volatile ("vbroadcasti128 %[k_ymm], %%ymm4\n\t" |
| 277 |
: |
| 278 |
: [k_ymm] "m" (consts->k_ymm[0]) |
| 279 |
); |
| 280 |
} |
| 281 |
|
| 282 |
/* Fold by 8. */ |
| 283 |
while (inlen >= 4 * 32) |
| 284 |
{ |
| 285 |
asm volatile ("vmovdqu %[inbuf_0], %%ymm5\n\t" |
| 286 |
"vmovdqa %%ymm0, %%ymm6\n\t" |
| 287 |
"vpclmulqdq $0x00, %%ymm4, %%ymm0, %%ymm0\n\t" |
| 288 |
"vpclmulqdq $0x11, %%ymm4, %%ymm6, %%ymm6\n\t" |
| 289 |
"vpxor %%ymm5, %%ymm0, %%ymm0\n\t" |
| 290 |
"vpxor %%ymm6, %%ymm0, %%ymm0\n\t" |
| 291 |
|
| 292 |
"vmovdqu %[inbuf_1], %%ymm5\n\t" |
| 293 |
"vmovdqa %%ymm1, %%ymm6\n\t" |
| 294 |
"vpclmulqdq $0x00, %%ymm4, %%ymm1, %%ymm1\n\t" |
| 295 |
"vpclmulqdq $0x11, %%ymm4, %%ymm6, %%ymm6\n\t" |
| 296 |
"vpxor %%ymm5, %%ymm1, %%ymm1\n\t" |
| 297 |
"vpxor %%ymm6, %%ymm1, %%ymm1\n\t" |
| 298 |
|
| 299 |
"vmovdqu %[inbuf_2], %%ymm5\n\t" |
| 300 |
"vmovdqa %%ymm2, %%ymm6\n\t" |
| 301 |
"vpclmulqdq $0x00, %%ymm4, %%ymm2, %%ymm2\n\t" |
| 302 |
"vpclmulqdq $0x11, %%ymm4, %%ymm6, %%ymm6\n\t" |
| 303 |
"vpxor %%ymm5, %%ymm2, %%ymm2\n\t" |
| 304 |
"vpxor %%ymm6, %%ymm2, %%ymm2\n\t" |
| 305 |
|
| 306 |
"vmovdqu %[inbuf_3], %%ymm5\n\t" |
| 307 |
"vmovdqa %%ymm3, %%ymm6\n\t" |
| 308 |
"vpclmulqdq $0x00, %%ymm4, %%ymm3, %%ymm3\n\t" |
| 309 |
"vpclmulqdq $0x11, %%ymm4, %%ymm6, %%ymm6\n\t" |
| 310 |
"vpxor %%ymm5, %%ymm3, %%ymm3\n\t" |
| 311 |
"vpxor %%ymm6, %%ymm3, %%ymm3\n\t" |
| 312 |
: |
| 313 |
: [inbuf_0] "m" (inbuf[0 * 32]), |
| 314 |
[inbuf_1] "m" (inbuf[1 * 32]), |
| 315 |
[inbuf_2] "m" (inbuf[2 * 32]), |
| 316 |
[inbuf_3] "m" (inbuf[3 * 32]) |
| 317 |
); |
| 318 |
|
| 319 |
inbuf += 4 * 32; |
| 320 |
inlen -= 4 * 32; |
| 321 |
} |
| 322 |
|
| 323 |
/* Fold 8 to 4. */ |
| 324 |
asm volatile("vbroadcasti128 %[k1k2], %%ymm4\n\t" |
| 325 |
|
| 326 |
/* Fold ymm2 into ymm0. */ |
| 327 |
"vmovdqa %%ymm0, %%ymm5\n\t" |
| 328 |
"vpclmulqdq $0x00, %%ymm4, %%ymm5, %%ymm5\n\t" |
| 329 |
"vpclmulqdq $0x11, %%ymm4, %%ymm0, %%ymm0\n\t" |
| 330 |
"vpxor %%ymm2, %%ymm5, %%ymm5\n\t" |
| 331 |
"vpxor %%ymm5, %%ymm0, %%ymm0\n\t" |
| 332 |
|
| 333 |
/* Fold ymm3 into ymm1. */ |
| 334 |
"vmovdqa %%ymm1, %%ymm5\n\t" |
| 335 |
"vpclmulqdq $0x00, %%ymm4, %%ymm5, %%ymm5\n\t" |
| 336 |
"vpclmulqdq $0x11, %%ymm4, %%ymm1, %%ymm1\n\t" |
| 337 |
"vpxor %%ymm3, %%ymm5, %%ymm5\n\t" |
| 338 |
"vpxor %%ymm5, %%ymm1, %%ymm1\n\t" |
| 339 |
|
| 340 |
"vextracti128 $1, %%ymm1, %%xmm3\n\t" |
| 341 |
"vmovdqa %%xmm1, %%xmm2\n\t" |
| 342 |
"vextracti128 $1, %%ymm0, %%xmm1\n\t" |
| 343 |
|
| 344 |
"vzeroupper\n\t" |
| 345 |
: |
| 346 |
: [k1k2] "m" (consts->k[1 - 1]) |
| 347 |
); |
| 348 |
} |
| 349 |
else |
| 350 |
{ |
| 351 |
asm volatile ("movd %[crc], %%xmm4\n\t" |
| 352 |
"movdqu %[inbuf_0], %%xmm0\n\t" |
| 353 |
"movdqu %[inbuf_1], %%xmm1\n\t" |
| 354 |
"movdqu %[inbuf_2], %%xmm2\n\t" |
| 355 |
"movdqu %[inbuf_3], %%xmm3\n\t" |
| 356 |
"pxor %%xmm4, %%xmm0\n\t" |
| 357 |
: |
| 358 |
: [inbuf_0] "m" (inbuf[0 * 16]), |
| 359 |
[inbuf_1] "m" (inbuf[1 * 16]), |
| 360 |
[inbuf_2] "m" (inbuf[2 * 16]), |
| 361 |
[inbuf_3] "m" (inbuf[3 * 16]), |
| 362 |
[crc] "m" (*pcrc) |
| 363 |
); |
| 364 |
|
| 365 |
inbuf += 4 * 16; |
| 366 |
inlen -= 4 * 16; |
| 367 |
|
| 368 |
asm volatile ("movdqa %[k1k2], %%xmm4\n\t" |
| 369 |
: |
| 370 |
: [k1k2] "m" (consts->k[1 - 1]) |
| 371 |
); |
| 372 |
} |
| 373 |
|
| 374 |
/* Fold by 4. */ |
| 375 |
while (inlen >= 4 * 16) |
| 376 |
{ |
| 377 |
asm volatile ("movdqu %[inbuf_0], %%xmm5\n\t" |
| 378 |
"movdqa %%xmm0, %%xmm6\n\t" |
| 379 |
"pclmulqdq $0x00, %%xmm4, %%xmm0\n\t" |
| 380 |
"pclmulqdq $0x11, %%xmm4, %%xmm6\n\t" |
| 381 |
"pxor %%xmm5, %%xmm0\n\t" |
| 382 |
"pxor %%xmm6, %%xmm0\n\t" |
| 383 |
|
| 384 |
"movdqu %[inbuf_1], %%xmm5\n\t" |
| 385 |
"movdqa %%xmm1, %%xmm6\n\t" |
| 386 |
"pclmulqdq $0x00, %%xmm4, %%xmm1\n\t" |
| 387 |
"pclmulqdq $0x11, %%xmm4, %%xmm6\n\t" |
| 388 |
"pxor %%xmm5, %%xmm1\n\t" |
| 389 |
"pxor %%xmm6, %%xmm1\n\t" |
| 390 |
|
| 391 |
"movdqu %[inbuf_2], %%xmm5\n\t" |
| 392 |
"movdqa %%xmm2, %%xmm6\n\t" |
| 393 |
"pclmulqdq $0x00, %%xmm4, %%xmm2\n\t" |
| 394 |
"pclmulqdq $0x11, %%xmm4, %%xmm6\n\t" |
| 395 |
"pxor %%xmm5, %%xmm2\n\t" |
| 396 |
"pxor %%xmm6, %%xmm2\n\t" |
| 397 |
|
| 398 |
"movdqu %[inbuf_3], %%xmm5\n\t" |
| 399 |
"movdqa %%xmm3, %%xmm6\n\t" |
| 400 |
"pclmulqdq $0x00, %%xmm4, %%xmm3\n\t" |
| 401 |
"pclmulqdq $0x11, %%xmm4, %%xmm6\n\t" |
| 402 |
"pxor %%xmm5, %%xmm3\n\t" |
| 403 |
"pxor %%xmm6, %%xmm3\n\t" |
| 404 |
: |
| 405 |
: [inbuf_0] "m" (inbuf[0 * 16]), |
| 406 |
[inbuf_1] "m" (inbuf[1 * 16]), |
| 407 |
[inbuf_2] "m" (inbuf[2 * 16]), |
| 408 |
[inbuf_3] "m" (inbuf[3 * 16]) |
| 409 |
); |
| 410 |
|
| 411 |
inbuf += 4 * 16; |
| 412 |
inlen -= 4 * 16; |
| 413 |
} |
| 414 |
|
| 415 |
asm volatile ("movdqa %[k3k4], %%xmm6\n\t" |
| 416 |
"movdqa %[my_p], %%xmm5\n\t" |
| 417 |
: |
| 418 |
: [k3k4] "m" (consts->k[3 - 1]), |
| 419 |
[my_p] "m" (consts->my_p[0]) |
| 420 |
); |
| 421 |
|
| 422 |
/* Fold 4 to 1. */ |
| 423 |
asm volatile ("movdqa %%xmm0, %%xmm4\n\t" |
| 424 |
"pclmulqdq $0x00, %%xmm6, %%xmm0\n\t" |
| 425 |
"pclmulqdq $0x11, %%xmm6, %%xmm4\n\t" |
| 426 |
"pxor %%xmm1, %%xmm0\n\t" |
| 427 |
"pxor %%xmm4, %%xmm0\n\t" |
| 428 |
|
| 429 |
"movdqa %%xmm0, %%xmm4\n\t" |
| 430 |
"pclmulqdq $0x00, %%xmm6, %%xmm0\n\t" |
| 431 |
"pclmulqdq $0x11, %%xmm6, %%xmm4\n\t" |
| 432 |
"pxor %%xmm2, %%xmm0\n\t" |
| 433 |
"pxor %%xmm4, %%xmm0\n\t" |
| 434 |
|
| 435 |
"movdqa %%xmm0, %%xmm4\n\t" |
| 436 |
"pclmulqdq $0x00, %%xmm6, %%xmm0\n\t" |
| 437 |
"pclmulqdq $0x11, %%xmm6, %%xmm4\n\t" |
| 438 |
"pxor %%xmm3, %%xmm0\n\t" |
| 439 |
"pxor %%xmm4, %%xmm0\n\t" |
| 440 |
: |
| 441 |
: |
| 442 |
); |
| 443 |
} |
| 444 |
else |
| 445 |
{ |
| 446 |
asm volatile ("movd %[crc], %%xmm1\n\t" |
| 447 |
"movdqu %[inbuf], %%xmm0\n\t" |
| 448 |
"movdqa %[k3k4], %%xmm6\n\t" |
| 449 |
"pxor %%xmm1, %%xmm0\n\t" |
| 450 |
"movdqa %[my_p], %%xmm5\n\t" |
| 451 |
: |
| 452 |
: [inbuf] "m" (*inbuf), |
| 453 |
[crc] "m" (*pcrc), |
| 454 |
[k3k4] "m" (consts->k[3 - 1]), |
| 455 |
[my_p] "m" (consts->my_p[0]) |
| 456 |
); |
| 457 |
|
| 458 |
inbuf += 16; |
| 459 |
inlen -= 16; |
| 460 |
} |
| 461 |
|
| 462 |
/* Fold by 1. */ |
| 463 |
if (inlen >= 16) |
| 464 |
{ |
| 465 |
while (inlen >= 16) |
| 466 |
{ |
| 467 |
/* Load next block to XMM2. Fold XMM0 to XMM0:XMM1. */ |
| 468 |
asm volatile ("movdqu %[inbuf], %%xmm2\n\t" |
| 469 |
"movdqa %%xmm0, %%xmm1\n\t" |
| 470 |
"pclmulqdq $0x00, %%xmm6, %%xmm0\n\t" |
| 471 |
"pclmulqdq $0x11, %%xmm6, %%xmm1\n\t" |
| 472 |
"pxor %%xmm2, %%xmm0\n\t" |
| 473 |
"pxor %%xmm1, %%xmm0\n\t" |
| 474 |
: |
| 475 |
: [inbuf] "m" (*inbuf) |
| 476 |
); |
| 477 |
|
| 478 |
inbuf += 16; |
| 479 |
inlen -= 16; |
| 480 |
} |
| 481 |
} |
| 482 |
|
| 483 |
/* Partial fold. */ |
| 484 |
if (inlen) |
| 485 |
{ |
| 486 |
/* Load last input and add padding zeros. */ |
| 487 |
asm volatile ("movdqu %[shr_shuf], %%xmm3\n\t" |
| 488 |
"movdqu %[shl_shuf], %%xmm4\n\t" |
| 489 |
"movdqu %[mask], %%xmm2\n\t" |
| 490 |
|
| 491 |
"movdqa %%xmm0, %%xmm1\n\t" |
| 492 |
"pshufb %%xmm4, %%xmm0\n\t" |
| 493 |
"movdqu %[inbuf], %%xmm4\n\t" |
| 494 |
"pshufb %%xmm3, %%xmm1\n\t" |
| 495 |
"pand %%xmm4, %%xmm2\n\t" |
| 496 |
"por %%xmm1, %%xmm2\n\t" |
| 497 |
|
| 498 |
"movdqa %%xmm0, %%xmm1\n\t" |
| 499 |
"pclmulqdq $0x00, %%xmm6, %%xmm0\n\t" |
| 500 |
"pclmulqdq $0x11, %%xmm6, %%xmm1\n\t" |
| 501 |
"pxor %%xmm2, %%xmm0\n\t" |
| 502 |
"pxor %%xmm1, %%xmm0\n\t" |
| 503 |
: |
| 504 |
: [inbuf] "m" (*(inbuf - 16 + inlen)), |
| 505 |
[mask] "m" (crc32_partial_fold_input_mask[inlen]), |
| 506 |
[shl_shuf] "m" (crc32_refl_shuf_shift[inlen]), |
| 507 |
[shr_shuf] "m" (crc32_refl_shuf_shift[inlen + 16]) |
| 508 |
); |
| 509 |
|
| 510 |
inbuf += inlen; |
| 511 |
inlen -= inlen; |
| 512 |
} |
| 513 |
|
| 514 |
/* Final fold. */ |
| 515 |
asm volatile (/* reduce 128-bits to 96-bits */ |
| 516 |
"movdqa %%xmm0, %%xmm1\n\t" |
| 517 |
"pclmulqdq $0x10, %%xmm6, %%xmm0\n\t" |
| 518 |
"psrldq $8, %%xmm1\n\t" |
| 519 |
"pxor %%xmm1, %%xmm0\n\t" |
| 520 |
|
| 521 |
/* reduce 96-bits to 64-bits */ |
| 522 |
"pshufd $0xfc, %%xmm0, %%xmm1\n\t" /* [00][00][00][x] */ |
| 523 |
"pshufd $0xf9, %%xmm0, %%xmm0\n\t" /* [00][00][x>>64][x>>32] */ |
| 524 |
"pclmulqdq $0x00, %[k5], %%xmm1\n\t" /* [00][00][xx][xx] */ |
| 525 |
"pxor %%xmm1, %%xmm0\n\t" /* top 64-bit are zero */ |
| 526 |
|
| 527 |
/* barrett reduction */ |
| 528 |
"pshufd $0xf3, %%xmm0, %%xmm1\n\t" /* [00][00][x>>32][00] */ |
| 529 |
"pslldq $4, %%xmm0\n\t" /* [??][x>>32][??][??] */ |
| 530 |
"pclmulqdq $0x00, %%xmm5, %%xmm1\n\t" /* [00][xx][xx][00] */ |
| 531 |
"pclmulqdq $0x10, %%xmm5, %%xmm1\n\t" /* [00][xx][xx][00] */ |
| 532 |
"pxor %%xmm1, %%xmm0\n\t" |
| 533 |
|
| 534 |
/* store CRC */ |
| 535 |
"pextrd $2, %%xmm0, %[out]\n\t" |
| 536 |
: [out] "=m" (*pcrc) |
| 537 |
: [k5] "m" (consts->k[5 - 1]) |
| 538 |
); |
| 539 |
} |
| 540 |
|
| 541 |
static ASM_FUNC_ATTR_INLINE void |
| 542 |
crc32_reflected_less_than_16 (u32 *pcrc, const byte *inbuf, size_t inlen, |
| 543 |
const struct crc32_consts_s *consts) |
| 544 |
{ |
| 545 |
if (inlen < 4) |
| 546 |
{ |
| 547 |
u32 crc = *pcrc; |
| 548 |
u32 data; |
| 549 |
|
| 550 |
asm volatile ("movdqa %[my_p], %%xmm5\n\t" |
| 551 |
: |
| 552 |
: [my_p] "m" (consts->my_p[0]) |
| 553 |
); |
| 554 |
|
| 555 |
if (inlen == 1) |
| 556 |
{ |
| 557 |
data = inbuf[0]; |
| 558 |
data ^= crc; |
| 559 |
data <<= 24; |
| 560 |
crc >>= 8; |
| 561 |
} |
| 562 |
else if (inlen == 2) |
| 563 |
{ |
| 564 |
data = ((const struct u16_unaligned_s *)inbuf)->a; |
| 565 |
data ^= crc; |
| 566 |
data <<= 16; |
| 567 |
crc >>= 16; |
| 568 |
} |
| 569 |
else |
| 570 |
{ |
| 571 |
data = ((const struct u16_unaligned_s *)inbuf)->a; |
| 572 |
data |= inbuf[2] << 16; |
| 573 |
data ^= crc; |
| 574 |
data <<= 8; |
| 575 |
crc >>= 24; |
| 576 |
} |
| 577 |
|
| 578 |
/* Barrett reduction */ |
| 579 |
asm volatile ("movd %[in], %%xmm0\n\t" |
| 580 |
"movd %[crc], %%xmm1\n\t" |
| 581 |
|
| 582 |
"pclmulqdq $0x00, %%xmm5, %%xmm0\n\t" /* [00][00][xx][xx] */ |
| 583 |
"psllq $32, %%xmm1\n\t" |
| 584 |
"pshufd $0xfc, %%xmm0, %%xmm0\n\t" /* [00][00][00][x] */ |
| 585 |
"pclmulqdq $0x10, %%xmm5, %%xmm0\n\t" /* [00][00][xx][xx] */ |
| 586 |
"pxor %%xmm1, %%xmm0\n\t" |
| 587 |
|
| 588 |
"pextrd $1, %%xmm0, %[out]\n\t" |
| 589 |
: [out] "=m" (*pcrc) |
| 590 |
: [in] "rm" (data), |
| 591 |
[crc] "rm" (crc) |
| 592 |
); |
| 593 |
} |
| 594 |
else if (inlen == 4) |
| 595 |
{ |
| 596 |
/* Barrett reduction */ |
| 597 |
asm volatile ("movd %[crc], %%xmm1\n\t" |
| 598 |
"movd %[in], %%xmm0\n\t" |
| 599 |
"movdqa %[my_p], %%xmm5\n\t" |
| 600 |
"pxor %%xmm1, %%xmm0\n\t" |
| 601 |
|
| 602 |
"pclmulqdq $0x00, %%xmm5, %%xmm0\n\t" /* [00][00][xx][xx] */ |
| 603 |
"pshufd $0xfc, %%xmm0, %%xmm0\n\t" /* [00][00][00][x] */ |
| 604 |
"pclmulqdq $0x10, %%xmm5, %%xmm0\n\t" /* [00][00][xx][xx] */ |
| 605 |
|
| 606 |
"pextrd $1, %%xmm0, %[out]\n\t" |
| 607 |
: [out] "=m" (*pcrc) |
| 608 |
: [in] "m" (*inbuf), |
| 609 |
[crc] "m" (*pcrc), |
| 610 |
[my_p] "m" (consts->my_p[0]) |
| 611 |
); |
| 612 |
} |
| 613 |
else |
| 614 |
{ |
| 615 |
asm volatile ("movdqu %[shuf], %%xmm4\n\t" |
| 616 |
"movd %[crc], %%xmm1\n\t" |
| 617 |
"movdqa %[my_p], %%xmm5\n\t" |
| 618 |
"movdqa %[k3k4], %%xmm6\n\t" |
| 619 |
: |
| 620 |
: [shuf] "m" (crc32_refl_shuf_shift[inlen]), |
| 621 |
[crc] "m" (*pcrc), |
| 622 |
[my_p] "m" (consts->my_p[0]), |
| 623 |
[k3k4] "m" (consts->k[3 - 1]) |
| 624 |
); |
| 625 |
|
| 626 |
if (inlen >= 8) |
| 627 |
{ |
| 628 |
asm volatile ("movq %[inbuf], %%xmm0\n\t" |
| 629 |
: |
| 630 |
: [inbuf] "m" (*inbuf) |
| 631 |
); |
| 632 |
if (inlen > 8) |
| 633 |
{ |
| 634 |
asm volatile (/*"pinsrq $1, %[inbuf_tail], %%xmm0\n\t"*/ |
| 635 |
"movq %[inbuf_tail], %%xmm2\n\t" |
| 636 |
"punpcklqdq %%xmm2, %%xmm0\n\t" |
| 637 |
"pshufb %[merge_shuf], %%xmm0\n\t" |
| 638 |
: |
| 639 |
: [inbuf_tail] "m" (inbuf[inlen - 8]), |
| 640 |
[merge_shuf] "m" |
| 641 |
(*crc32_merge9to15_shuf[inlen - 9]) |
| 642 |
); |
| 643 |
} |
| 644 |
} |
| 645 |
else |
| 646 |
{ |
| 647 |
asm volatile ("movd %[inbuf], %%xmm0\n\t" |
| 648 |
"pinsrd $1, %[inbuf_tail], %%xmm0\n\t" |
| 649 |
"pshufb %[merge_shuf], %%xmm0\n\t" |
| 650 |
: |
| 651 |
: [inbuf] "m" (*inbuf), |
| 652 |
[inbuf_tail] "m" (inbuf[inlen - 4]), |
| 653 |
[merge_shuf] "m" |
| 654 |
(*crc32_merge5to7_shuf[inlen - 5]) |
| 655 |
); |
| 656 |
} |
| 657 |
|
| 658 |
/* Final fold. */ |
| 659 |
asm volatile ("pxor %%xmm1, %%xmm0\n\t" |
| 660 |
"pshufb %%xmm4, %%xmm0\n\t" |
| 661 |
|
| 662 |
/* reduce 128-bits to 96-bits */ |
| 663 |
"movdqa %%xmm0, %%xmm1\n\t" |
| 664 |
"pclmulqdq $0x10, %%xmm6, %%xmm0\n\t" |
| 665 |
"psrldq $8, %%xmm1\n\t" |
| 666 |
"pxor %%xmm1, %%xmm0\n\t" /* top 32-bit are zero */ |
| 667 |
|
| 668 |
/* reduce 96-bits to 64-bits */ |
| 669 |
"pshufd $0xfc, %%xmm0, %%xmm1\n\t" /* [00][00][00][x] */ |
| 670 |
"pshufd $0xf9, %%xmm0, %%xmm0\n\t" /* [00][00][x>>64][x>>32] */ |
| 671 |
"pclmulqdq $0x00, %[k5], %%xmm1\n\t" /* [00][00][xx][xx] */ |
| 672 |
"pxor %%xmm1, %%xmm0\n\t" /* top 64-bit are zero */ |
| 673 |
|
| 674 |
/* barrett reduction */ |
| 675 |
"pshufd $0xf3, %%xmm0, %%xmm1\n\t" /* [00][00][x>>32][00] */ |
| 676 |
"pslldq $4, %%xmm0\n\t" /* [??][x>>32][??][??] */ |
| 677 |
"pclmulqdq $0x00, %%xmm5, %%xmm1\n\t" /* [00][xx][xx][00] */ |
| 678 |
"pclmulqdq $0x10, %%xmm5, %%xmm1\n\t" /* [00][xx][xx][00] */ |
| 679 |
"pxor %%xmm1, %%xmm0\n\t" |
| 680 |
|
| 681 |
/* store CRC */ |
| 682 |
"pextrd $2, %%xmm0, %[out]\n\t" |
| 683 |
: [out] "=m" (*pcrc) |
| 684 |
: [k5] "m" (consts->k[5 - 1]) |
| 685 |
); |
| 686 |
} |
| 687 |
} |
| 688 |
|
| 689 |
/* PCLMUL functions for non-reflected CRC32. */ |
| 690 |
static ASM_FUNC_ATTR_INLINE void |
| 691 |
crc32_bulk (u32 *pcrc, const byte *inbuf, size_t inlen, |
| 692 |
const struct crc32_consts_s *consts, u32 hwfeatures) |
| 693 |
{ |
| 694 |
asm volatile ("movdqa %[bswap], %%xmm7\n\t" |
| 695 |
: |
| 696 |
: [bswap] "m" (*crc32_bswap_shuf) |
| 697 |
); |
| 698 |
|
| 699 |
if (inlen >= 8 * 16) |
| 700 |
{ |
| 701 |
if ((hwfeatures & HWF_INTEL_VAES_VPCLMUL) |
| 702 |
&& (hwfeatures & HWF_INTEL_AVX2) |
| 703 |
&& inlen >= 8 * 32) |
| 704 |
{ |
| 705 |
if ((hwfeatures & HWF_INTEL_VAES_VPCLMUL) |
| 706 |
&& (hwfeatures & HWF_INTEL_AVX512) |
| 707 |
&& inlen >= 8 * 64) |
| 708 |
{ |
| 709 |
asm volatile("vpopcntb %%xmm7, %%xmm0\n\t" /* spec stop for old AVX512 CPUs */ |
| 710 |
"vshufi32x4 $0x00, %%zmm7, %%zmm7, %%zmm7\n\t" |
| 711 |
"vmovd %[crc], %%xmm4\n\t" |
| 712 |
"vmovdqu64 %[inbuf_0], %%zmm0\n\t" |
| 713 |
"vmovdqu64 %[inbuf_1], %%zmm1\n\t" |
| 714 |
"vmovdqu64 %[inbuf_2], %%zmm2\n\t" |
| 715 |
"vmovdqu64 %[inbuf_3], %%zmm3\n\t" |
| 716 |
"vpxorq %%zmm4, %%zmm0, %%zmm0\n\t" |
| 717 |
"vpshufb %%zmm7, %%zmm0, %%zmm0\n\t" |
| 718 |
"vpshufb %%zmm7, %%zmm1, %%zmm1\n\t" |
| 719 |
"vpshufb %%zmm7, %%zmm2, %%zmm2\n\t" |
| 720 |
"vpshufb %%zmm7, %%zmm3, %%zmm3\n\t" |
| 721 |
: |
| 722 |
: [crc] "m" (*pcrc), |
| 723 |
[inbuf_0] "m" (inbuf[0 * 64]), |
| 724 |
[inbuf_1] "m" (inbuf[1 * 64]), |
| 725 |
[inbuf_2] "m" (inbuf[2 * 64]), |
| 726 |
[inbuf_3] "m" (inbuf[3 * 64]) |
| 727 |
); |
| 728 |
|
| 729 |
inbuf += 4 * 64; |
| 730 |
inlen -= 4 * 64; |
| 731 |
|
| 732 |
asm volatile ("vbroadcasti32x4 %[k_zmm], %%zmm4\n\t" |
| 733 |
: |
| 734 |
: [k_zmm] "m" (consts->k_zmm[0]) |
| 735 |
); |
| 736 |
|
| 737 |
/* Fold by 16. */ |
| 738 |
while (inlen >= 4 * 64) |
| 739 |
{ |
| 740 |
asm volatile ("vmovdqu64 %[inbuf_0], %%zmm5\n\t" |
| 741 |
"vmovdqa64 %%zmm0, %%zmm6\n\t" |
| 742 |
"vpshufb %%zmm7, %%zmm5, %%zmm5\n\t" |
| 743 |
"vpclmulqdq $0x01, %%zmm4, %%zmm0, %%zmm0\n\t" |
| 744 |
"vpclmulqdq $0x10, %%zmm4, %%zmm6, %%zmm6\n\t" |
| 745 |
"vpternlogq $0x96, %%zmm5, %%zmm6, %%zmm0\n\t" |
| 746 |
|
| 747 |
"vmovdqu64 %[inbuf_1], %%zmm5\n\t" |
| 748 |
"vmovdqa64 %%zmm1, %%zmm6\n\t" |
| 749 |
"vpshufb %%zmm7, %%zmm5, %%zmm5\n\t" |
| 750 |
"vpclmulqdq $0x01, %%zmm4, %%zmm1, %%zmm1\n\t" |
| 751 |
"vpclmulqdq $0x10, %%zmm4, %%zmm6, %%zmm6\n\t" |
| 752 |
"vpternlogq $0x96, %%zmm5, %%zmm6, %%zmm1\n\t" |
| 753 |
|
| 754 |
"vmovdqu64 %[inbuf_2], %%zmm5\n\t" |
| 755 |
"vmovdqa64 %%zmm2, %%zmm6\n\t" |
| 756 |
"vpshufb %%zmm7, %%zmm5, %%zmm5\n\t" |
| 757 |
"vpclmulqdq $0x01, %%zmm4, %%zmm2, %%zmm2\n\t" |
| 758 |
"vpclmulqdq $0x10, %%zmm4, %%zmm6, %%zmm6\n\t" |
| 759 |
"vpternlogq $0x96, %%zmm5, %%zmm6, %%zmm2\n\t" |
| 760 |
|
| 761 |
"vmovdqu64 %[inbuf_3], %%zmm5\n\t" |
| 762 |
"vmovdqa64 %%zmm3, %%zmm6\n\t" |
| 763 |
"vpshufb %%zmm7, %%zmm5, %%zmm5\n\t" |
| 764 |
"vpclmulqdq $0x01, %%zmm4, %%zmm3, %%zmm3\n\t" |
| 765 |
"vpclmulqdq $0x10, %%zmm4, %%zmm6, %%zmm6\n\t" |
| 766 |
"vpternlogq $0x96, %%zmm5, %%zmm6, %%zmm3\n\t" |
| 767 |
: |
| 768 |
: [inbuf_0] "m" (inbuf[0 * 64]), |
| 769 |
[inbuf_1] "m" (inbuf[1 * 64]), |
| 770 |
[inbuf_2] "m" (inbuf[2 * 64]), |
| 771 |
[inbuf_3] "m" (inbuf[3 * 64]) |
| 772 |
); |
| 773 |
|
| 774 |
inbuf += 4 * 64; |
| 775 |
inlen -= 4 * 64; |
| 776 |
} |
| 777 |
|
| 778 |
asm volatile("vbroadcasti32x4 %[k_ymm], %%zmm4\n\t" |
| 779 |
/* Fold zmm2 into zmm0. */ |
| 780 |
"vmovdqa64 %%zmm0, %%zmm5\n\t" |
| 781 |
"vpclmulqdq $0x01, %%zmm4, %%zmm5, %%zmm5\n\t" |
| 782 |
"vpclmulqdq $0x10, %%zmm4, %%zmm0, %%zmm0\n\t" |
| 783 |
"vpternlogq $0x96, %%zmm2, %%zmm5, %%zmm0\n\t" |
| 784 |
/* Fold zmm3 into zmm1. */ |
| 785 |
"vmovdqa64 %%zmm1, %%zmm5\n\t" |
| 786 |
"vpclmulqdq $0x01, %%zmm4, %%zmm5, %%zmm5\n\t" |
| 787 |
"vpclmulqdq $0x10, %%zmm4, %%zmm1, %%zmm1\n\t" |
| 788 |
"vpternlogq $0x96, %%zmm3, %%zmm5, %%zmm1\n\t" |
| 789 |
: |
| 790 |
: [k_ymm] "m" (consts->k_ymm[0]) |
| 791 |
); |
| 792 |
|
| 793 |
asm volatile("vextracti64x4 $1, %%zmm1, %%ymm3\n\t" |
| 794 |
"vmovdqa %%ymm1, %%ymm2\n\t" |
| 795 |
"vextracti64x4 $1, %%zmm0, %%ymm1\n\t" |
| 796 |
: |
| 797 |
: |
| 798 |
); |
| 799 |
} |
| 800 |
else |
| 801 |
{ |
| 802 |
asm volatile("vinserti128 $1, %%xmm7, %%ymm7, %%ymm7\n\t" |
| 803 |
"vmovd %[crc], %%xmm4\n\t" |
| 804 |
"vmovdqu %[inbuf_0], %%ymm0\n\t" |
| 805 |
"vmovdqu %[inbuf_1], %%ymm1\n\t" |
| 806 |
"vmovdqu %[inbuf_2], %%ymm2\n\t" |
| 807 |
"vmovdqu %[inbuf_3], %%ymm3\n\t" |
| 808 |
"vpxor %%ymm4, %%ymm0, %%ymm0\n\t" |
| 809 |
"vpshufb %%ymm7, %%ymm0, %%ymm0\n\t" |
| 810 |
"vpshufb %%ymm7, %%ymm1, %%ymm1\n\t" |
| 811 |
"vpshufb %%ymm7, %%ymm2, %%ymm2\n\t" |
| 812 |
"vpshufb %%ymm7, %%ymm3, %%ymm3\n\t" |
| 813 |
: |
| 814 |
: [crc] "m" (*pcrc), |
| 815 |
[inbuf_0] "m" (inbuf[0 * 32]), |
| 816 |
[inbuf_1] "m" (inbuf[1 * 32]), |
| 817 |
[inbuf_2] "m" (inbuf[2 * 32]), |
| 818 |
[inbuf_3] "m" (inbuf[3 * 32]) |
| 819 |
); |
| 820 |
|
| 821 |
inbuf += 4 * 32; |
| 822 |
inlen -= 4 * 32; |
| 823 |
|
| 824 |
asm volatile ("vbroadcasti128 %[k_ymm], %%ymm4\n\t" |
| 825 |
: : [k_ymm] "m" (consts->k_ymm[0])); |
| 826 |
} |
| 827 |
|
| 828 |
/* Fold by 8. */ |
| 829 |
while (inlen >= 4 * 32) |
| 830 |
{ |
| 831 |
asm volatile ("vmovdqu %[inbuf_0], %%ymm5\n\t" |
| 832 |
"vmovdqa %%ymm0, %%ymm6\n\t" |
| 833 |
"vpshufb %%ymm7, %%ymm5, %%ymm5\n\t" |
| 834 |
"vpclmulqdq $0x01, %%ymm4, %%ymm0, %%ymm0\n\t" |
| 835 |
"vpclmulqdq $0x10, %%ymm4, %%ymm6, %%ymm6\n\t" |
| 836 |
"vpxor %%ymm5, %%ymm0, %%ymm0\n\t" |
| 837 |
"vpxor %%ymm6, %%ymm0, %%ymm0\n\t" |
| 838 |
|
| 839 |
"vmovdqu %[inbuf_1], %%ymm5\n\t" |
| 840 |
"vmovdqa %%ymm1, %%ymm6\n\t" |
| 841 |
"vpshufb %%ymm7, %%ymm5, %%ymm5\n\t" |
| 842 |
"vpclmulqdq $0x01, %%ymm4, %%ymm1, %%ymm1\n\t" |
| 843 |
"vpclmulqdq $0x10, %%ymm4, %%ymm6, %%ymm6\n\t" |
| 844 |
"vpxor %%ymm5, %%ymm1, %%ymm1\n\t" |
| 845 |
"vpxor %%ymm6, %%ymm1, %%ymm1\n\t" |
| 846 |
|
| 847 |
"vmovdqu %[inbuf_2], %%ymm5\n\t" |
| 848 |
"vmovdqa %%ymm2, %%ymm6\n\t" |
| 849 |
"vpshufb %%ymm7, %%ymm5, %%ymm5\n\t" |
| 850 |
"vpclmulqdq $0x01, %%ymm4, %%ymm2, %%ymm2\n\t" |
| 851 |
"vpclmulqdq $0x10, %%ymm4, %%ymm6, %%ymm6\n\t" |
| 852 |
"vpxor %%ymm5, %%ymm2, %%ymm2\n\t" |
| 853 |
"vpxor %%ymm6, %%ymm2, %%ymm2\n\t" |
| 854 |
|
| 855 |
"vmovdqu %[inbuf_3], %%ymm5\n\t" |
| 856 |
"vmovdqa %%ymm3, %%ymm6\n\t" |
| 857 |
"vpshufb %%ymm7, %%ymm5, %%ymm5\n\t" |
| 858 |
"vpclmulqdq $0x01, %%ymm4, %%ymm3, %%ymm3\n\t" |
| 859 |
"vpclmulqdq $0x10, %%ymm4, %%ymm6, %%ymm6\n\t" |
| 860 |
"vpxor %%ymm5, %%ymm3, %%ymm3\n\t" |
| 861 |
"vpxor %%ymm6, %%ymm3, %%ymm3\n\t" |
| 862 |
: |
| 863 |
: [inbuf_0] "m" (inbuf[0 * 32]), |
| 864 |
[inbuf_1] "m" (inbuf[1 * 32]), |
| 865 |
[inbuf_2] "m" (inbuf[2 * 32]), |
| 866 |
[inbuf_3] "m" (inbuf[3 * 32]) |
| 867 |
); |
| 868 |
|
| 869 |
inbuf += 4 * 32; |
| 870 |
inlen -= 4 * 32; |
| 871 |
} |
| 872 |
|
| 873 |
asm volatile("vbroadcasti128 %[k1k2], %%ymm4\n\t" |
| 874 |
|
| 875 |
/* Fold ymm2 into ymm0. */ |
| 876 |
"vmovdqa %%ymm0, %%ymm5\n\t" |
| 877 |
"vpclmulqdq $0x01, %%ymm4, %%ymm5, %%ymm5\n\t" |
| 878 |
"vpclmulqdq $0x10, %%ymm4, %%ymm0, %%ymm0\n\t" |
| 879 |
"vpxor %%ymm2, %%ymm5, %%ymm5\n\t" |
| 880 |
"vpxor %%ymm5, %%ymm0, %%ymm0\n\t" |
| 881 |
|
| 882 |
/* Fold ymm3 into ymm1. */ |
| 883 |
"vmovdqa %%ymm1, %%ymm5\n\t" |
| 884 |
"vpclmulqdq $0x01, %%ymm4, %%ymm5, %%ymm5\n\t" |
| 885 |
"vpclmulqdq $0x10, %%ymm4, %%ymm1, %%ymm1\n\t" |
| 886 |
"vpxor %%ymm3, %%ymm5, %%ymm5\n\t" |
| 887 |
"vpxor %%ymm5, %%ymm1, %%ymm1\n\t" |
| 888 |
|
| 889 |
"vextracti128 $1, %%ymm1, %%xmm3\n\t" |
| 890 |
"vmovdqa %%xmm1, %%xmm2\n\t" |
| 891 |
"vextracti128 $1, %%ymm0, %%xmm1\n\t" |
| 892 |
"vzeroupper\n\t" |
| 893 |
: |
| 894 |
: [k1k2] "m" (consts->k[1 - 1]) |
| 895 |
); |
| 896 |
} |
| 897 |
else |
| 898 |
{ |
| 899 |
asm volatile ("movd %[crc], %%xmm4\n\t" |
| 900 |
"movdqu %[inbuf_0], %%xmm0\n\t" |
| 901 |
"movdqu %[inbuf_1], %%xmm1\n\t" |
| 902 |
"movdqu %[inbuf_2], %%xmm2\n\t" |
| 903 |
"pxor %%xmm4, %%xmm0\n\t" |
| 904 |
"movdqu %[inbuf_3], %%xmm3\n\t" |
| 905 |
"pshufb %%xmm7, %%xmm0\n\t" |
| 906 |
"pshufb %%xmm7, %%xmm1\n\t" |
| 907 |
"pshufb %%xmm7, %%xmm2\n\t" |
| 908 |
"pshufb %%xmm7, %%xmm3\n\t" |
| 909 |
: |
| 910 |
: [inbuf_0] "m" (inbuf[0 * 16]), |
| 911 |
[inbuf_1] "m" (inbuf[1 * 16]), |
| 912 |
[inbuf_2] "m" (inbuf[2 * 16]), |
| 913 |
[inbuf_3] "m" (inbuf[3 * 16]), |
| 914 |
[crc] "m" (*pcrc) |
| 915 |
); |
| 916 |
|
| 917 |
inbuf += 4 * 16; |
| 918 |
inlen -= 4 * 16; |
| 919 |
|
| 920 |
asm volatile ("movdqa %[k1k2], %%xmm4\n\t" |
| 921 |
: |
| 922 |
: [k1k2] "m" (consts->k[1 - 1]) |
| 923 |
); |
| 924 |
} |
| 925 |
|
| 926 |
/* Fold by 4. */ |
| 927 |
while (inlen >= 4 * 16) |
| 928 |
{ |
| 929 |
asm volatile ("movdqu %[inbuf_0], %%xmm5\n\t" |
| 930 |
"movdqa %%xmm0, %%xmm6\n\t" |
| 931 |
"pshufb %%xmm7, %%xmm5\n\t" |
| 932 |
"pclmulqdq $0x01, %%xmm4, %%xmm0\n\t" |
| 933 |
"pclmulqdq $0x10, %%xmm4, %%xmm6\n\t" |
| 934 |
"pxor %%xmm5, %%xmm0\n\t" |
| 935 |
"pxor %%xmm6, %%xmm0\n\t" |
| 936 |
|
| 937 |
"movdqu %[inbuf_1], %%xmm5\n\t" |
| 938 |
"movdqa %%xmm1, %%xmm6\n\t" |
| 939 |
"pshufb %%xmm7, %%xmm5\n\t" |
| 940 |
"pclmulqdq $0x01, %%xmm4, %%xmm1\n\t" |
| 941 |
"pclmulqdq $0x10, %%xmm4, %%xmm6\n\t" |
| 942 |
"pxor %%xmm5, %%xmm1\n\t" |
| 943 |
"pxor %%xmm6, %%xmm1\n\t" |
| 944 |
|
| 945 |
"movdqu %[inbuf_2], %%xmm5\n\t" |
| 946 |
"movdqa %%xmm2, %%xmm6\n\t" |
| 947 |
"pshufb %%xmm7, %%xmm5\n\t" |
| 948 |
"pclmulqdq $0x01, %%xmm4, %%xmm2\n\t" |
| 949 |
"pclmulqdq $0x10, %%xmm4, %%xmm6\n\t" |
| 950 |
"pxor %%xmm5, %%xmm2\n\t" |
| 951 |
"pxor %%xmm6, %%xmm2\n\t" |
| 952 |
|
| 953 |
"movdqu %[inbuf_3], %%xmm5\n\t" |
| 954 |
"movdqa %%xmm3, %%xmm6\n\t" |
| 955 |
"pshufb %%xmm7, %%xmm5\n\t" |
| 956 |
"pclmulqdq $0x01, %%xmm4, %%xmm3\n\t" |
| 957 |
"pclmulqdq $0x10, %%xmm4, %%xmm6\n\t" |
| 958 |
"pxor %%xmm5, %%xmm3\n\t" |
| 959 |
"pxor %%xmm6, %%xmm3\n\t" |
| 960 |
: |
| 961 |
: [inbuf_0] "m" (inbuf[0 * 16]), |
| 962 |
[inbuf_1] "m" (inbuf[1 * 16]), |
| 963 |
[inbuf_2] "m" (inbuf[2 * 16]), |
| 964 |
[inbuf_3] "m" (inbuf[3 * 16]) |
| 965 |
); |
| 966 |
|
| 967 |
inbuf += 4 * 16; |
| 968 |
inlen -= 4 * 16; |
| 969 |
} |
| 970 |
|
| 971 |
asm volatile ("movdqa %[k3k4], %%xmm6\n\t" |
| 972 |
"movdqa %[my_p], %%xmm5\n\t" |
| 973 |
: |
| 974 |
: [k3k4] "m" (consts->k[3 - 1]), |
| 975 |
[my_p] "m" (consts->my_p[0]) |
| 976 |
); |
| 977 |
|
| 978 |
/* Fold 4 to 1. */ |
| 979 |
asm volatile ("movdqa %%xmm0, %%xmm4\n\t" |
| 980 |
"pclmulqdq $0x01, %%xmm6, %%xmm0\n\t" |
| 981 |
"pclmulqdq $0x10, %%xmm6, %%xmm4\n\t" |
| 982 |
"pxor %%xmm1, %%xmm0\n\t" |
| 983 |
"pxor %%xmm4, %%xmm0\n\t" |
| 984 |
|
| 985 |
"movdqa %%xmm0, %%xmm4\n\t" |
| 986 |
"pclmulqdq $0x01, %%xmm6, %%xmm0\n\t" |
| 987 |
"pclmulqdq $0x10, %%xmm6, %%xmm4\n\t" |
| 988 |
"pxor %%xmm2, %%xmm0\n\t" |
| 989 |
"pxor %%xmm4, %%xmm0\n\t" |
| 990 |
|
| 991 |
"movdqa %%xmm0, %%xmm4\n\t" |
| 992 |
"pclmulqdq $0x01, %%xmm6, %%xmm0\n\t" |
| 993 |
"pclmulqdq $0x10, %%xmm6, %%xmm4\n\t" |
| 994 |
"pxor %%xmm3, %%xmm0\n\t" |
| 995 |
"pxor %%xmm4, %%xmm0\n\t" |
| 996 |
: |
| 997 |
: |
| 998 |
); |
| 999 |
} |
| 1000 |
else |
| 1001 |
{ |
| 1002 |
asm volatile ("movd %[crc], %%xmm1\n\t" |
| 1003 |
"movdqu %[inbuf], %%xmm0\n\t" |
| 1004 |
"movdqa %[k3k4], %%xmm6\n\t" |
| 1005 |
"pxor %%xmm1, %%xmm0\n\t" |
| 1006 |
"movdqa %[my_p], %%xmm5\n\t" |
| 1007 |
"pshufb %%xmm7, %%xmm0\n\t" |
| 1008 |
: |
| 1009 |
: [inbuf] "m" (*inbuf), |
| 1010 |
[crc] "m" (*pcrc), |
| 1011 |
[k3k4] "m" (consts->k[3 - 1]), |
| 1012 |
[my_p] "m" (consts->my_p[0]) |
| 1013 |
); |
| 1014 |
|
| 1015 |
inbuf += 16; |
| 1016 |
inlen -= 16; |
| 1017 |
} |
| 1018 |
|
| 1019 |
/* Fold by 1. */ |
| 1020 |
if (inlen >= 16) |
| 1021 |
{ |
| 1022 |
while (inlen >= 16) |
| 1023 |
{ |
| 1024 |
/* Load next block to XMM2. Fold XMM0 to XMM0:XMM1. */ |
| 1025 |
asm volatile ("movdqu %[inbuf], %%xmm2\n\t" |
| 1026 |
"movdqa %%xmm0, %%xmm1\n\t" |
| 1027 |
"pclmulqdq $0x01, %%xmm6, %%xmm0\n\t" |
| 1028 |
"pshufb %%xmm7, %%xmm2\n\t" |
| 1029 |
"pclmulqdq $0x10, %%xmm6, %%xmm1\n\t" |
| 1030 |
"pxor %%xmm2, %%xmm0\n\t" |
| 1031 |
"pxor %%xmm1, %%xmm0\n\t" |
| 1032 |
: |
| 1033 |
: [inbuf] "m" (*inbuf) |
| 1034 |
); |
| 1035 |
|
| 1036 |
inbuf += 16; |
| 1037 |
inlen -= 16; |
| 1038 |
} |
| 1039 |
} |
| 1040 |
|
| 1041 |
/* Partial fold. */ |
| 1042 |
if (inlen) |
| 1043 |
{ |
| 1044 |
/* Load last input and add padding zeros. */ |
| 1045 |
asm volatile ("movdqu %[shl_shuf], %%xmm4\n\t" |
| 1046 |
"movdqu %[shr_shuf], %%xmm3\n\t" |
| 1047 |
"movdqu %[mask], %%xmm2\n\t" |
| 1048 |
|
| 1049 |
"movdqa %%xmm0, %%xmm1\n\t" |
| 1050 |
"pshufb %%xmm4, %%xmm0\n\t" |
| 1051 |
"movdqu %[inbuf], %%xmm4\n\t" |
| 1052 |
"pshufb %%xmm3, %%xmm1\n\t" |
| 1053 |
"pand %%xmm4, %%xmm2\n\t" |
| 1054 |
"por %%xmm1, %%xmm2\n\t" |
| 1055 |
|
| 1056 |
"pshufb %%xmm7, %%xmm2\n\t" |
| 1057 |
|
| 1058 |
"movdqa %%xmm0, %%xmm1\n\t" |
| 1059 |
"pclmulqdq $0x01, %%xmm6, %%xmm0\n\t" |
| 1060 |
"pclmulqdq $0x10, %%xmm6, %%xmm1\n\t" |
| 1061 |
"pxor %%xmm2, %%xmm0\n\t" |
| 1062 |
"pxor %%xmm1, %%xmm0\n\t" |
| 1063 |
: |
| 1064 |
: [inbuf] "m" (*(inbuf - 16 + inlen)), |
| 1065 |
[mask] "m" (crc32_partial_fold_input_mask[inlen]), |
| 1066 |
[shl_shuf] "m" (crc32_refl_shuf_shift[32 - inlen]), |
| 1067 |
[shr_shuf] "m" (crc32_shuf_shift[inlen + 16]) |
| 1068 |
); |
| 1069 |
|
| 1070 |
inbuf += inlen; |
| 1071 |
inlen -= inlen; |
| 1072 |
} |
| 1073 |
|
| 1074 |
/* Final fold. */ |
| 1075 |
asm volatile (/* reduce 128-bits to 96-bits */ |
| 1076 |
"movdqa %%xmm0, %%xmm1\n\t" |
| 1077 |
"pclmulqdq $0x11, %%xmm6, %%xmm0\n\t" |
| 1078 |
"pslldq $8, %%xmm1\n\t" |
| 1079 |
"pxor %%xmm1, %%xmm0\n\t" /* bottom 32-bit are zero */ |
| 1080 |
|
| 1081 |
/* reduce 96-bits to 64-bits */ |
| 1082 |
"pshufd $0x30, %%xmm0, %%xmm1\n\t" /* [00][x>>96][00][00] */ |
| 1083 |
"pshufd $0x24, %%xmm0, %%xmm0\n\t" /* [00][xx][xx][00] */ |
| 1084 |
"pclmulqdq $0x01, %[k5], %%xmm1\n\t" /* [00][xx][xx][00] */ |
| 1085 |
"pxor %%xmm1, %%xmm0\n\t" /* top and bottom 32-bit are zero */ |
| 1086 |
|
| 1087 |
/* barrett reduction */ |
| 1088 |
"pshufd $0x01, %%xmm0, %%xmm1\n\t" /* [00][00][00][x>>32] */ |
| 1089 |
"pclmulqdq $0x01, %%xmm5, %%xmm0\n\t" /* [00][xx][xx][xx] */ |
| 1090 |
"psrldq $4, %%xmm0\n\t" /* [00][00][xx][xx] */ |
| 1091 |
"pclmulqdq $0x10, %%xmm5, %%xmm0\n\t" |
| 1092 |
"pxor %%xmm1, %%xmm0\n\t" |
| 1093 |
|
| 1094 |
/* store CRC in input endian */ |
| 1095 |
"movd %%xmm0, %%eax\n\t" |
| 1096 |
"bswapl %%eax\n\t" |
| 1097 |
"movl %%eax, %[out]\n\t" |
| 1098 |
: [out] "=m" (*pcrc) |
| 1099 |
: [k5] "m" (consts->k[5 - 1]) |
| 1100 |
: "eax" ); |
| 1101 |
} |
| 1102 |
|
| 1103 |
static ASM_FUNC_ATTR_INLINE void |
| 1104 |
crc32_less_than_16 (u32 *pcrc, const byte *inbuf, size_t inlen, |
| 1105 |
const struct crc32_consts_s *consts) |
| 1106 |
{ |
| 1107 |
if (inlen < 4) |
| 1108 |
{ |
| 1109 |
u32 crc = *pcrc; |
| 1110 |
u32 data; |
| 1111 |
|
| 1112 |
asm volatile ("movdqa %[my_p], %%xmm5\n\t" |
| 1113 |
: |
| 1114 |
: [my_p] "m" (consts->my_p[0]) |
| 1115 |
); |
| 1116 |
|
| 1117 |
if (inlen == 1) |
| 1118 |
{ |
| 1119 |
data = inbuf[0]; |
| 1120 |
data ^= crc; |
| 1121 |
data = _gcry_bswap32(data << 24); |
| 1122 |
crc = _gcry_bswap32(crc >> 8); |
| 1123 |
} |
| 1124 |
else if (inlen == 2) |
| 1125 |
{ |
| 1126 |
data = ((const struct u16_unaligned_s *)inbuf)->a; |
| 1127 |
data ^= crc; |
| 1128 |
data = _gcry_bswap32(data << 16); |
| 1129 |
crc = _gcry_bswap32(crc >> 16); |
| 1130 |
} |
| 1131 |
else |
| 1132 |
{ |
| 1133 |
data = ((const struct u16_unaligned_s *)inbuf)->a; |
| 1134 |
data |= inbuf[2] << 16; |
| 1135 |
data ^= crc; |
| 1136 |
data = _gcry_bswap32(data << 8); |
| 1137 |
crc = _gcry_bswap32(crc >> 24); |
| 1138 |
} |
| 1139 |
|
| 1140 |
/* Barrett reduction */ |
| 1141 |
asm volatile ("movd %[in], %%xmm0\n\t" |
| 1142 |
"psllq $32, %%xmm0\n\t" /* [00][00][xx][00] */ |
| 1143 |
"movd %[crc], %%xmm1\n\t" |
| 1144 |
|
| 1145 |
"pclmulqdq $0x00, %%xmm5, %%xmm0\n\t" /* [00][xx][xx][00] */ |
| 1146 |
"pclmulqdq $0x11, %%xmm5, %%xmm0\n\t" /* [00][00][xx][xx] */ |
| 1147 |
"pxor %%xmm1, %%xmm0\n\t" |
| 1148 |
|
| 1149 |
/* store CRC in input endian */ |
| 1150 |
"movd %%xmm0, %%eax\n\t" |
| 1151 |
"bswapl %%eax\n\t" |
| 1152 |
"movl %%eax, %[out]\n\t" |
| 1153 |
: [out] "=m" (*pcrc) |
| 1154 |
: [in] "r" (data), |
| 1155 |
[crc] "r" (crc) |
| 1156 |
: "eax" ); |
| 1157 |
} |
| 1158 |
else if (inlen == 4) |
| 1159 |
{ |
| 1160 |
/* Barrett reduction */ |
| 1161 |
asm volatile ("movd %[crc], %%xmm0\n\t" |
| 1162 |
"movd %[in], %%xmm1\n\t" |
| 1163 |
"movdqa %[my_p], %%xmm5\n\t" |
| 1164 |
: |
| 1165 |
: [in] "m" (*inbuf), |
| 1166 |
[crc] "m" (*pcrc), |
| 1167 |
[my_p] "m" (consts->my_p[0]) |
| 1168 |
: "cc" ); |
| 1169 |
|
| 1170 |
asm volatile ("pxor %%xmm1, %%xmm0\n\t" |
| 1171 |
"pshufb %[bswap], %%xmm0\n\t" /* [xx][00][00][00] */ |
| 1172 |
|
| 1173 |
"pclmulqdq $0x01, %%xmm5, %%xmm0\n\t" /* [00][xx][xx][00] */ |
| 1174 |
"pclmulqdq $0x11, %%xmm5, %%xmm0\n\t" /* [00][00][xx][xx] */ |
| 1175 |
: |
| 1176 |
: [bswap] "m" (*crc32_bswap_shuf) |
| 1177 |
: "cc" ); |
| 1178 |
|
| 1179 |
asm volatile (/* store CRC in input endian */ |
| 1180 |
"movd %%xmm0, %%eax\n\t" |
| 1181 |
"bswapl %%eax\n\t" |
| 1182 |
"movl %%eax, %[out]\n\t" |
| 1183 |
: [out] "=m" (*pcrc) |
| 1184 |
: |
| 1185 |
: "eax", "cc" ); |
| 1186 |
} |
| 1187 |
else |
| 1188 |
{ |
| 1189 |
asm volatile ("movdqu %[shuf], %%xmm7\n\t" |
| 1190 |
"movd %[crc], %%xmm1\n\t" |
| 1191 |
"movdqa %[my_p], %%xmm5\n\t" |
| 1192 |
"movdqa %[k3k4], %%xmm6\n\t" |
| 1193 |
: |
| 1194 |
: [shuf] "m" (crc32_shuf_shift[32 - inlen]), |
| 1195 |
[crc] "m" (*pcrc), |
| 1196 |
[my_p] "m" (consts->my_p[0]), |
| 1197 |
[k3k4] "m" (consts->k[3 - 1]) |
| 1198 |
); |
| 1199 |
|
| 1200 |
if (inlen >= 8) |
| 1201 |
{ |
| 1202 |
asm volatile ("movq %[inbuf], %%xmm0\n\t" |
| 1203 |
: |
| 1204 |
: [inbuf] "m" (*inbuf) |
| 1205 |
); |
| 1206 |
if (inlen > 8) |
| 1207 |
{ |
| 1208 |
asm volatile (/*"pinsrq $1, %[inbuf_tail], %%xmm0\n\t"*/ |
| 1209 |
"movq %[inbuf_tail], %%xmm2\n\t" |
| 1210 |
"punpcklqdq %%xmm2, %%xmm0\n\t" |
| 1211 |
"pshufb %[merge_shuf], %%xmm0\n\t" |
| 1212 |
: |
| 1213 |
: [inbuf_tail] "m" (inbuf[inlen - 8]), |
| 1214 |
[merge_shuf] "m" |
| 1215 |
(*crc32_merge9to15_shuf[inlen - 9]) |
| 1216 |
); |
| 1217 |
} |
| 1218 |
} |
| 1219 |
else |
| 1220 |
{ |
| 1221 |
asm volatile ("movd %[inbuf], %%xmm0\n\t" |
| 1222 |
"pinsrd $1, %[inbuf_tail], %%xmm0\n\t" |
| 1223 |
"pshufb %[merge_shuf], %%xmm0\n\t" |
| 1224 |
: |
| 1225 |
: [inbuf] "m" (*inbuf), |
| 1226 |
[inbuf_tail] "m" (inbuf[inlen - 4]), |
| 1227 |
[merge_shuf] "m" |
| 1228 |
(*crc32_merge5to7_shuf[inlen - 5]) |
| 1229 |
); |
| 1230 |
} |
| 1231 |
|
| 1232 |
/* Final fold. */ |
| 1233 |
asm volatile ("pxor %%xmm1, %%xmm0\n\t" |
| 1234 |
"pshufb %%xmm7, %%xmm0\n\t" |
| 1235 |
|
| 1236 |
/* reduce 128-bits to 96-bits */ |
| 1237 |
"movdqa %%xmm0, %%xmm1\n\t" |
| 1238 |
"pclmulqdq $0x11, %%xmm6, %%xmm0\n\t" |
| 1239 |
"pslldq $8, %%xmm1\n\t" |
| 1240 |
"pxor %%xmm1, %%xmm0\n\t" /* bottom 32-bit are zero */ |
| 1241 |
|
| 1242 |
/* reduce 96-bits to 64-bits */ |
| 1243 |
"pshufd $0x30, %%xmm0, %%xmm1\n\t" /* [00][x>>96][00][00] */ |
| 1244 |
"pshufd $0x24, %%xmm0, %%xmm0\n\t" /* [00][xx][xx][00] */ |
| 1245 |
"pclmulqdq $0x01, %[k5], %%xmm1\n\t" /* [00][xx][xx][00] */ |
| 1246 |
"pxor %%xmm1, %%xmm0\n\t" /* top and bottom 32-bit are zero */ |
| 1247 |
|
| 1248 |
/* barrett reduction */ |
| 1249 |
"pshufd $0x01, %%xmm0, %%xmm1\n\t" /* [00][00][00][x>>32] */ |
| 1250 |
"pclmulqdq $0x01, %%xmm5, %%xmm0\n\t" /* [00][xx][xx][xx] */ |
| 1251 |
"psrldq $4, %%xmm0\n\t" /* [00][00][xx][xx] */ |
| 1252 |
"pclmulqdq $0x10, %%xmm5, %%xmm0\n\t" |
| 1253 |
"pxor %%xmm1, %%xmm0\n\t" |
| 1254 |
|
| 1255 |
/* store CRC in input endian */ |
| 1256 |
"movd %%xmm0, %%eax\n\t" |
| 1257 |
"bswapl %%eax\n\t" |
| 1258 |
"movl %%eax, %[out]\n\t" |
| 1259 |
: [out] "=m" (*pcrc) |
| 1260 |
: [k5] "m" (consts->k[5 - 1]) |
| 1261 |
: "eax" ); |
| 1262 |
} |
| 1263 |
} |
| 1264 |
|
| 1265 |
void ASM_FUNC_ATTR |
| 1266 |
_gcry_crc32_intel_pclmul (u32 *pcrc, const byte *inbuf, size_t inlen, |
| 1267 |
u32 hwfeatures) |
| 1268 |
{ |
| 1269 |
const struct crc32_consts_s *consts = &crc32_consts; |
| 1270 |
#if defined(__x86_64__) && defined(__WIN64__) |
| 1271 |
char win64tmp[2 * 16]; |
| 1272 |
|
| 1273 |
/* XMM6-XMM7 need to be restored after use. */ |
| 1274 |
asm volatile ("movdqu %%xmm6, 0*16(%0)\n\t" |
| 1275 |
"movdqu %%xmm7, 1*16(%0)\n\t" |
| 1276 |
: |
| 1277 |
: "r" (win64tmp) |
| 1278 |
: "memory"); |
| 1279 |
#endif |
| 1280 |
|
| 1281 |
if (!inlen) |
| 1282 |
return; |
| 1283 |
|
| 1284 |
if (inlen >= 16) |
| 1285 |
crc32_reflected_bulk(pcrc, inbuf, inlen, consts, hwfeatures); |
| 1286 |
else |
| 1287 |
crc32_reflected_less_than_16(pcrc, inbuf, inlen, consts); |
| 1288 |
|
| 1289 |
#if defined(__x86_64__) && defined(__WIN64__) |
| 1290 |
/* Restore used registers. */ |
| 1291 |
asm volatile("movdqu 0*16(%0), %%xmm6\n\t" |
| 1292 |
"movdqu 1*16(%0), %%xmm7\n\t" |
| 1293 |
: |
| 1294 |
: "r" (win64tmp) |
| 1295 |
: "memory"); |
| 1296 |
#endif |
| 1297 |
} |
| 1298 |
|
| 1299 |
void ASM_FUNC_ATTR |
| 1300 |
_gcry_crc24rfc2440_intel_pclmul (u32 *pcrc, const byte *inbuf, size_t inlen, |
| 1301 |
u32 hwfeatures) |
| 1302 |
{ |
| 1303 |
const struct crc32_consts_s *consts = &crc24rfc2440_consts; |
| 1304 |
#if defined(__x86_64__) && defined(__WIN64__) |
| 1305 |
char win64tmp[2 * 16]; |
| 1306 |
|
| 1307 |
/* XMM6-XMM7 need to be restored after use. */ |
| 1308 |
asm volatile ("movdqu %%xmm6, 0*16(%0)\n\t" |
| 1309 |
"movdqu %%xmm7, 1*16(%0)\n\t" |
| 1310 |
: |
| 1311 |
: "r" (win64tmp) |
| 1312 |
: "memory"); |
| 1313 |
#endif |
| 1314 |
|
| 1315 |
if (!inlen) |
| 1316 |
return; |
| 1317 |
|
| 1318 |
/* Note: *pcrc in input endian. */ |
| 1319 |
|
| 1320 |
if (inlen >= 16) |
| 1321 |
crc32_bulk(pcrc, inbuf, inlen, consts, hwfeatures); |
| 1322 |
else |
| 1323 |
crc32_less_than_16(pcrc, inbuf, inlen, consts); |
| 1324 |
|
| 1325 |
#if defined(__x86_64__) && defined(__WIN64__) |
| 1326 |
/* Restore used registers. */ |
| 1327 |
asm volatile("movdqu 0*16(%0), %%xmm6\n\t" |
| 1328 |
"movdqu 1*16(%0), %%xmm7\n\t" |
| 1329 |
: |
| 1330 |
: "r" (win64tmp) |
| 1331 |
: "memory"); |
| 1332 |
#endif |
| 1333 |
} |
| 1334 |
|
| 1335 |
#if __clang__ |
| 1336 |
# pragma clang attribute pop |
| 1337 |
#endif |
| 1338 |
|
| 1339 |
#endif /* USE_INTEL_PCLMUL */ |