| 1 |
/* crc-ppc.c - POWER8 vpmsum accelerated CRC implementation |
| 2 |
* Copyright (C) 2019-2020 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_PPC_CRYPTO_SUPPORT) && \ |
| 34 |
defined(HAVE_COMPATIBLE_CC_PPC_ALTIVEC) && \ |
| 35 |
defined(HAVE_GCC_INLINE_ASM_PPC_ALTIVEC) && \ |
| 36 |
__GNUC__ >= 4 |
| 37 |
|
| 38 |
#include <altivec.h> |
| 39 |
#include "bufhelp.h" |
| 40 |
|
| 41 |
|
| 42 |
#define ALWAYS_INLINE inline __attribute__((always_inline)) |
| 43 |
#define NO_INLINE __attribute__((noinline)) |
| 44 |
#define NO_INSTRUMENT_FUNCTION __attribute__((no_instrument_function)) |
| 45 |
|
| 46 |
#define ASM_FUNC_ATTR NO_INSTRUMENT_FUNCTION |
| 47 |
#define ASM_FUNC_ATTR_INLINE ASM_FUNC_ATTR ALWAYS_INLINE |
| 48 |
#define ASM_FUNC_ATTR_NOINLINE ASM_FUNC_ATTR NO_INLINE |
| 49 |
|
| 50 |
#define ALIGNED_64 __attribute__ ((aligned (64))) |
| 51 |
|
| 52 |
|
| 53 |
typedef vector unsigned char vector16x_u8; |
| 54 |
typedef vector unsigned int vector4x_u32; |
| 55 |
typedef vector unsigned long long vector2x_u64; |
| 56 |
|
| 57 |
|
| 58 |
/* Constants structure for generic reflected/non-reflected CRC32 PMULL |
| 59 |
* functions. */ |
| 60 |
struct crc32_consts_s |
| 61 |
{ |
| 62 |
/* k: { x^(32*17), x^(32*15), x^(32*5), x^(32*3), x^(32*2), 0 } mod P(x) */ |
| 63 |
unsigned long long k[6]; |
| 64 |
/* my_p: { floor(x^64 / P(x)), P(x) } */ |
| 65 |
unsigned long long my_p[2]; |
| 66 |
}; |
| 67 |
|
| 68 |
/* PMULL constants for CRC32 and CRC32RFC1510. */ |
| 69 |
static const struct crc32_consts_s crc32_consts ALIGNED_64 = |
| 70 |
{ |
| 71 |
{ /* k[6] = reverse_33bits( x^(32*y) mod P(x) ) */ |
| 72 |
U64_C(0x154442bd4), U64_C(0x1c6e41596), /* y = { 17, 15 } */ |
| 73 |
U64_C(0x1751997d0), U64_C(0x0ccaa009e), /* y = { 5, 3 } */ |
| 74 |
U64_C(0x163cd6124), 0 /* y = 2 */ |
| 75 |
}, |
| 76 |
{ /* my_p[2] = reverse_33bits ( { floor(x^64 / P(x)), P(x) } ) */ |
| 77 |
U64_C(0x1f7011641), U64_C(0x1db710641) |
| 78 |
} |
| 79 |
}; |
| 80 |
|
| 81 |
/* PMULL constants for CRC24RFC2440 (polynomial multiplied with x⁸). */ |
| 82 |
static const struct crc32_consts_s crc24rfc2440_consts ALIGNED_64 = |
| 83 |
{ |
| 84 |
{ /* k[6] = x^(32*y) mod P(x) << 32*/ |
| 85 |
U64_C(0x08289a00) << 32, U64_C(0x74b44a00) << 32, /* y = { 17, 15 } */ |
| 86 |
U64_C(0xc4b14d00) << 32, U64_C(0xfd7e0c00) << 32, /* y = { 5, 3 } */ |
| 87 |
U64_C(0xd9fe8c00) << 32, 0 /* y = 2 */ |
| 88 |
}, |
| 89 |
{ /* my_p[2] = { floor(x^64 / P(x)), P(x) } */ |
| 90 |
U64_C(0x1f845fe24), U64_C(0x1864cfb00) |
| 91 |
} |
| 92 |
}; |
| 93 |
|
| 94 |
|
| 95 |
static ASM_FUNC_ATTR_INLINE vector2x_u64 |
| 96 |
asm_vpmsumd(vector2x_u64 a, vector2x_u64 b) |
| 97 |
{ |
| 98 |
__asm__("vpmsumd %0, %1, %2" |
| 99 |
: "=v" (a) |
| 100 |
: "v" (a), "v" (b)); |
| 101 |
return a; |
| 102 |
} |
| 103 |
|
| 104 |
|
| 105 |
static ASM_FUNC_ATTR_INLINE vector2x_u64 |
| 106 |
asm_swap_u64(vector2x_u64 a) |
| 107 |
{ |
| 108 |
__asm__("xxswapd %x0, %x1" |
| 109 |
: "=wa" (a) |
| 110 |
: "wa" (a)); |
| 111 |
return a; |
| 112 |
} |
| 113 |
|
| 114 |
|
| 115 |
static ASM_FUNC_ATTR_INLINE vector4x_u32 |
| 116 |
vec_sld_u32(vector4x_u32 a, vector4x_u32 b, unsigned int idx) |
| 117 |
{ |
| 118 |
return vec_sld (a, b, (4 * idx) & 15); |
| 119 |
} |
| 120 |
|
| 121 |
|
| 122 |
static const byte crc32_partial_fold_input_mask[16 + 16] ALIGNED_64 = |
| 123 |
{ |
| 124 |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 125 |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 126 |
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 127 |
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 128 |
}; |
| 129 |
static const byte crc32_shuf_shift[3 * 16] ALIGNED_64 = |
| 130 |
{ |
| 131 |
0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, |
| 132 |
0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, |
| 133 |
0x0f, 0x0e, 0x0d, 0x0c, 0x0b, 0x0a, 0x09, 0x08, |
| 134 |
0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01, 0x00, |
| 135 |
0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, |
| 136 |
0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, |
| 137 |
}; |
| 138 |
static const byte crc32_refl_shuf_shift[3 * 16] ALIGNED_64 = |
| 139 |
{ |
| 140 |
0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, |
| 141 |
0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, |
| 142 |
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, |
| 143 |
0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, |
| 144 |
0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, |
| 145 |
0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, |
| 146 |
}; |
| 147 |
static const vector16x_u8 bswap_const ALIGNED_64 = |
| 148 |
{ 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 }; |
| 149 |
|
| 150 |
|
| 151 |
#define CRC_VEC_SWAP(v) ({ vector2x_u64 __vecu64 = (v); \ |
| 152 |
vec_perm(__vecu64, __vecu64, bswap_const); }) |
| 153 |
|
| 154 |
#ifdef WORDS_BIGENDIAN |
| 155 |
# define CRC_VEC_U64_DEF(lo, hi) { (hi), (lo) } |
| 156 |
# define CRC_VEC_U64_LOAD(offs, ptr) \ |
| 157 |
asm_swap_u64(asm_vec_u64_load(offs, ptr)) |
| 158 |
# define CRC_VEC_U64_LOAD_LE(offs, ptr) \ |
| 159 |
CRC_VEC_SWAP(asm_vec_u64_load(offs, ptr)) |
| 160 |
# define CRC_VEC_U64_LOAD_BE(offs, ptr) \ |
| 161 |
asm_vec_u64_load(offs, ptr) |
| 162 |
# define CRC_VEC_SWAP_TO_LE(v) CRC_VEC_SWAP(v) |
| 163 |
# define CRC_VEC_SWAP_TO_BE(v) (v) |
| 164 |
# define VEC_U64_LO 1 |
| 165 |
# define VEC_U64_HI 0 |
| 166 |
|
| 167 |
static ASM_FUNC_ATTR_INLINE vector2x_u64 |
| 168 |
asm_vec_u64_load(unsigned long offset, const void *ptr) |
| 169 |
{ |
| 170 |
vector2x_u64 vecu64; |
| 171 |
#if __GNUC__ >= 4 |
| 172 |
if (__builtin_constant_p (offset) && offset == 0) |
| 173 |
__asm__ volatile ("lxvd2x %x0,0,%1\n\t" |
| 174 |
: "=wa" (vecu64) |
| 175 |
: "r" ((uintptr_t)ptr) |
| 176 |
: "memory"); |
| 177 |
else |
| 178 |
#endif |
| 179 |
__asm__ volatile ("lxvd2x %x0,%1,%2\n\t" |
| 180 |
: "=wa" (vecu64) |
| 181 |
: "r" (offset), "r" ((uintptr_t)ptr) |
| 182 |
: "memory", "r0"); |
| 183 |
return vecu64; |
| 184 |
} |
| 185 |
#else |
| 186 |
# define CRC_VEC_U64_DEF(lo, hi) { (lo), (hi) } |
| 187 |
# define CRC_VEC_U64_LOAD(offs, ptr) asm_vec_u64_load_le(offs, ptr) |
| 188 |
# define CRC_VEC_U64_LOAD_LE(offs, ptr) asm_vec_u64_load_le(offs, ptr) |
| 189 |
# define CRC_VEC_U64_LOAD_BE(offs, ptr) asm_vec_u64_load_be(offs, ptr) |
| 190 |
# define CRC_VEC_SWAP_TO_LE(v) (v) |
| 191 |
# define CRC_VEC_SWAP_TO_BE(v) CRC_VEC_SWAP(v) |
| 192 |
# define VEC_U64_LO 0 |
| 193 |
# define VEC_U64_HI 1 |
| 194 |
|
| 195 |
static ASM_FUNC_ATTR_INLINE vector2x_u64 |
| 196 |
asm_vec_u64_load_le(unsigned long offset, const void *ptr) |
| 197 |
{ |
| 198 |
vector2x_u64 vecu64; |
| 199 |
#if __GNUC__ >= 4 |
| 200 |
if (__builtin_constant_p (offset) && offset == 0) |
| 201 |
__asm__ volatile ("lxvd2x %x0,0,%1\n\t" |
| 202 |
: "=wa" (vecu64) |
| 203 |
: "r" ((uintptr_t)ptr) |
| 204 |
: "memory"); |
| 205 |
else |
| 206 |
#endif |
| 207 |
__asm__ volatile ("lxvd2x %x0,%1,%2\n\t" |
| 208 |
: "=wa" (vecu64) |
| 209 |
: "r" (offset), "r" ((uintptr_t)ptr) |
| 210 |
: "memory", "r0"); |
| 211 |
return asm_swap_u64(vecu64); |
| 212 |
} |
| 213 |
|
| 214 |
static ASM_FUNC_ATTR_INLINE vector2x_u64 |
| 215 |
asm_vec_u64_load_be(unsigned int offset, const void *ptr) |
| 216 |
{ |
| 217 |
static const vector16x_u8 vec_load_le_const = |
| 218 |
{ ~7, ~6, ~5, ~4, ~3, ~2, ~1, ~0, ~15, ~14, ~13, ~12, ~11, ~10, ~9, ~8 }; |
| 219 |
vector2x_u64 vecu64; |
| 220 |
|
| 221 |
#if __GNUC__ >= 4 |
| 222 |
if (__builtin_constant_p (offset) && offset == 0) |
| 223 |
__asm__ ("lxvd2x %%vs32,0,%1\n\t" |
| 224 |
"vperm %0,%%v0,%%v0,%2\n\t" |
| 225 |
: "=v" (vecu64) |
| 226 |
: "r" ((uintptr_t)(ptr)), "v" (vec_load_le_const) |
| 227 |
: "memory", "v0"); |
| 228 |
#endif |
| 229 |
else |
| 230 |
__asm__ ("lxvd2x %%vs32,%1,%2\n\t" |
| 231 |
"vperm %0,%%v0,%%v0,%3\n\t" |
| 232 |
: "=v" (vecu64) |
| 233 |
: "r" (offset), "r" ((uintptr_t)(ptr)), |
| 234 |
"v" (vec_load_le_const) |
| 235 |
: "memory", "r0", "v0"); |
| 236 |
|
| 237 |
return vecu64; |
| 238 |
} |
| 239 |
#endif |
| 240 |
|
| 241 |
|
| 242 |
static ASM_FUNC_ATTR_INLINE void |
| 243 |
crc32r_ppc8_ce_bulk (u32 *pcrc, const byte *inbuf, size_t inlen, |
| 244 |
const struct crc32_consts_s *consts) |
| 245 |
{ |
| 246 |
vector4x_u32 zero = { 0, 0, 0, 0 }; |
| 247 |
vector2x_u64 low_64bit_mask = CRC_VEC_U64_DEF((u64)-1, 0); |
| 248 |
vector2x_u64 low_32bit_mask = CRC_VEC_U64_DEF((u32)-1, 0); |
| 249 |
vector2x_u64 my_p = CRC_VEC_U64_LOAD(0, &consts->my_p[0]); |
| 250 |
vector2x_u64 k1k2 = CRC_VEC_U64_LOAD(0, &consts->k[1 - 1]); |
| 251 |
vector2x_u64 k3k4 = CRC_VEC_U64_LOAD(0, &consts->k[3 - 1]); |
| 252 |
vector2x_u64 k4lo = CRC_VEC_U64_DEF(k3k4[VEC_U64_HI], 0); |
| 253 |
vector2x_u64 k5lo = CRC_VEC_U64_LOAD(0, &consts->k[5 - 1]); |
| 254 |
vector2x_u64 crc = CRC_VEC_U64_DEF(*pcrc, 0); |
| 255 |
vector2x_u64 crc0, crc1, crc2, crc3; |
| 256 |
vector2x_u64 v0; |
| 257 |
|
| 258 |
if (inlen >= 8 * 16) |
| 259 |
{ |
| 260 |
crc0 = CRC_VEC_U64_LOAD_LE(0 * 16, inbuf); |
| 261 |
crc0 ^= crc; |
| 262 |
crc1 = CRC_VEC_U64_LOAD_LE(1 * 16, inbuf); |
| 263 |
crc2 = CRC_VEC_U64_LOAD_LE(2 * 16, inbuf); |
| 264 |
crc3 = CRC_VEC_U64_LOAD_LE(3 * 16, inbuf); |
| 265 |
|
| 266 |
inbuf += 4 * 16; |
| 267 |
inlen -= 4 * 16; |
| 268 |
|
| 269 |
/* Fold by 4. */ |
| 270 |
while (inlen >= 4 * 16) |
| 271 |
{ |
| 272 |
v0 = CRC_VEC_U64_LOAD_LE(0 * 16, inbuf); |
| 273 |
crc0 = asm_vpmsumd(crc0, k1k2) ^ v0; |
| 274 |
|
| 275 |
v0 = CRC_VEC_U64_LOAD_LE(1 * 16, inbuf); |
| 276 |
crc1 = asm_vpmsumd(crc1, k1k2) ^ v0; |
| 277 |
|
| 278 |
v0 = CRC_VEC_U64_LOAD_LE(2 * 16, inbuf); |
| 279 |
crc2 = asm_vpmsumd(crc2, k1k2) ^ v0; |
| 280 |
|
| 281 |
v0 = CRC_VEC_U64_LOAD_LE(3 * 16, inbuf); |
| 282 |
crc3 = asm_vpmsumd(crc3, k1k2) ^ v0; |
| 283 |
|
| 284 |
inbuf += 4 * 16; |
| 285 |
inlen -= 4 * 16; |
| 286 |
} |
| 287 |
|
| 288 |
/* Fold 4 to 1. */ |
| 289 |
crc1 ^= asm_vpmsumd(crc0, k3k4); |
| 290 |
crc2 ^= asm_vpmsumd(crc1, k3k4); |
| 291 |
crc3 ^= asm_vpmsumd(crc2, k3k4); |
| 292 |
crc = crc3; |
| 293 |
} |
| 294 |
else |
| 295 |
{ |
| 296 |
v0 = CRC_VEC_U64_LOAD_LE(0, inbuf); |
| 297 |
crc ^= v0; |
| 298 |
|
| 299 |
inbuf += 16; |
| 300 |
inlen -= 16; |
| 301 |
} |
| 302 |
|
| 303 |
/* Fold by 1. */ |
| 304 |
while (inlen >= 16) |
| 305 |
{ |
| 306 |
v0 = CRC_VEC_U64_LOAD_LE(0, inbuf); |
| 307 |
crc = asm_vpmsumd(k3k4, crc); |
| 308 |
crc ^= v0; |
| 309 |
|
| 310 |
inbuf += 16; |
| 311 |
inlen -= 16; |
| 312 |
} |
| 313 |
|
| 314 |
/* Partial fold. */ |
| 315 |
if (inlen) |
| 316 |
{ |
| 317 |
/* Load last input and add padding zeros. */ |
| 318 |
vector2x_u64 mask = CRC_VEC_U64_LOAD_LE(inlen, crc32_partial_fold_input_mask); |
| 319 |
vector2x_u64 shl_shuf = CRC_VEC_U64_LOAD_LE(inlen, crc32_refl_shuf_shift); |
| 320 |
vector2x_u64 shr_shuf = CRC_VEC_U64_LOAD_LE(inlen + 16, crc32_refl_shuf_shift); |
| 321 |
|
| 322 |
v0 = CRC_VEC_U64_LOAD_LE(inlen - 16, inbuf); |
| 323 |
v0 &= mask; |
| 324 |
|
| 325 |
crc = CRC_VEC_SWAP_TO_LE(crc); |
| 326 |
v0 |= (vector2x_u64)vec_perm((vector16x_u8)crc, (vector16x_u8)zero, |
| 327 |
(vector16x_u8)shr_shuf); |
| 328 |
crc = (vector2x_u64)vec_perm((vector16x_u8)crc, (vector16x_u8)zero, |
| 329 |
(vector16x_u8)shl_shuf); |
| 330 |
crc = asm_vpmsumd(k3k4, crc); |
| 331 |
crc ^= v0; |
| 332 |
|
| 333 |
inbuf += inlen; |
| 334 |
inlen -= inlen; |
| 335 |
} |
| 336 |
|
| 337 |
/* Final fold. */ |
| 338 |
|
| 339 |
/* reduce 128-bits to 96-bits */ |
| 340 |
v0 = asm_swap_u64(crc); |
| 341 |
v0 &= low_64bit_mask; |
| 342 |
crc = asm_vpmsumd(k4lo, crc); |
| 343 |
crc ^= v0; |
| 344 |
|
| 345 |
/* reduce 96-bits to 64-bits */ |
| 346 |
v0 = (vector2x_u64)vec_sld_u32((vector4x_u32)crc, |
| 347 |
(vector4x_u32)crc, 3); /* [x0][x3][x2][x1] */ |
| 348 |
v0 &= low_64bit_mask; /* [00][00][x2][x1] */ |
| 349 |
crc = crc & low_32bit_mask; /* [00][00][00][x0] */ |
| 350 |
crc = v0 ^ asm_vpmsumd(k5lo, crc); /* [00][00][xx][xx] */ |
| 351 |
|
| 352 |
/* barrett reduction */ |
| 353 |
v0 = crc << 32; /* [00][00][x0][00] */ |
| 354 |
v0 = asm_vpmsumd(my_p, v0); |
| 355 |
v0 = asm_swap_u64(v0); |
| 356 |
v0 = asm_vpmsumd(my_p, v0); |
| 357 |
crc = (vector2x_u64)vec_sld_u32((vector4x_u32)crc, |
| 358 |
zero, 1); /* [00][x1][x0][00] */ |
| 359 |
crc ^= v0; |
| 360 |
|
| 361 |
*pcrc = (u32)crc[VEC_U64_HI]; |
| 362 |
} |
| 363 |
|
| 364 |
|
| 365 |
static ASM_FUNC_ATTR_INLINE u32 |
| 366 |
crc32r_ppc8_ce_reduction_4 (u32 data, u32 crc, |
| 367 |
const struct crc32_consts_s *consts) |
| 368 |
{ |
| 369 |
vector4x_u32 zero = { 0, 0, 0, 0 }; |
| 370 |
vector2x_u64 my_p = CRC_VEC_U64_LOAD(0, &consts->my_p[0]); |
| 371 |
vector2x_u64 v0 = CRC_VEC_U64_DEF((u64)data, 0); |
| 372 |
v0 = asm_vpmsumd(v0, my_p); /* [00][00][xx][xx] */ |
| 373 |
v0 = (vector2x_u64)vec_sld_u32((vector4x_u32)v0, |
| 374 |
zero, 3); /* [x0][00][00][00] */ |
| 375 |
v0 = (vector2x_u64)vec_sld_u32((vector4x_u32)v0, |
| 376 |
(vector4x_u32)v0, 3); /* [00][x0][00][00] */ |
| 377 |
v0 = asm_vpmsumd(v0, my_p); /* [00][00][xx][xx] */ |
| 378 |
return (v0[VEC_U64_LO] >> 32) ^ crc; |
| 379 |
} |
| 380 |
|
| 381 |
|
| 382 |
static ASM_FUNC_ATTR_INLINE void |
| 383 |
crc32r_less_than_16 (u32 *pcrc, const byte *inbuf, size_t inlen, |
| 384 |
const struct crc32_consts_s *consts) |
| 385 |
{ |
| 386 |
u32 crc = *pcrc; |
| 387 |
u32 data; |
| 388 |
|
| 389 |
while (inlen >= 4) |
| 390 |
{ |
| 391 |
data = buf_get_le32(inbuf); |
| 392 |
data ^= crc; |
| 393 |
|
| 394 |
inlen -= 4; |
| 395 |
inbuf += 4; |
| 396 |
|
| 397 |
crc = crc32r_ppc8_ce_reduction_4 (data, 0, consts); |
| 398 |
} |
| 399 |
|
| 400 |
switch (inlen) |
| 401 |
{ |
| 402 |
case 0: |
| 403 |
break; |
| 404 |
case 1: |
| 405 |
data = inbuf[0]; |
| 406 |
data ^= crc; |
| 407 |
data <<= 24; |
| 408 |
crc >>= 8; |
| 409 |
crc = crc32r_ppc8_ce_reduction_4 (data, crc, consts); |
| 410 |
break; |
| 411 |
case 2: |
| 412 |
data = inbuf[0] << 0; |
| 413 |
data |= inbuf[1] << 8; |
| 414 |
data ^= crc; |
| 415 |
data <<= 16; |
| 416 |
crc >>= 16; |
| 417 |
crc = crc32r_ppc8_ce_reduction_4 (data, crc, consts); |
| 418 |
break; |
| 419 |
case 3: |
| 420 |
data = inbuf[0] << 0; |
| 421 |
data |= inbuf[1] << 8; |
| 422 |
data |= inbuf[2] << 16; |
| 423 |
data ^= crc; |
| 424 |
data <<= 8; |
| 425 |
crc >>= 24; |
| 426 |
crc = crc32r_ppc8_ce_reduction_4 (data, crc, consts); |
| 427 |
break; |
| 428 |
} |
| 429 |
|
| 430 |
*pcrc = crc; |
| 431 |
} |
| 432 |
|
| 433 |
|
| 434 |
static ASM_FUNC_ATTR_INLINE void |
| 435 |
crc32_ppc8_ce_bulk (u32 *pcrc, const byte *inbuf, size_t inlen, |
| 436 |
const struct crc32_consts_s *consts) |
| 437 |
{ |
| 438 |
vector4x_u32 zero = { 0, 0, 0, 0 }; |
| 439 |
vector2x_u64 low_96bit_mask = CRC_VEC_U64_DEF(~0, ~((u64)(u32)-1 << 32)); |
| 440 |
vector2x_u64 p_my = asm_swap_u64(CRC_VEC_U64_LOAD(0, &consts->my_p[0])); |
| 441 |
vector2x_u64 p_my_lo, p_my_hi; |
| 442 |
vector2x_u64 k2k1 = asm_swap_u64(CRC_VEC_U64_LOAD(0, &consts->k[1 - 1])); |
| 443 |
vector2x_u64 k4k3 = asm_swap_u64(CRC_VEC_U64_LOAD(0, &consts->k[3 - 1])); |
| 444 |
vector2x_u64 k4hi = CRC_VEC_U64_DEF(0, consts->k[4 - 1]); |
| 445 |
vector2x_u64 k5hi = CRC_VEC_U64_DEF(0, consts->k[5 - 1]); |
| 446 |
vector2x_u64 crc = CRC_VEC_U64_DEF(0, _gcry_bswap64(*pcrc)); |
| 447 |
vector2x_u64 crc0, crc1, crc2, crc3; |
| 448 |
vector2x_u64 v0; |
| 449 |
|
| 450 |
if (inlen >= 8 * 16) |
| 451 |
{ |
| 452 |
crc0 = CRC_VEC_U64_LOAD_BE(0 * 16, inbuf); |
| 453 |
crc0 ^= crc; |
| 454 |
crc1 = CRC_VEC_U64_LOAD_BE(1 * 16, inbuf); |
| 455 |
crc2 = CRC_VEC_U64_LOAD_BE(2 * 16, inbuf); |
| 456 |
crc3 = CRC_VEC_U64_LOAD_BE(3 * 16, inbuf); |
| 457 |
|
| 458 |
inbuf += 4 * 16; |
| 459 |
inlen -= 4 * 16; |
| 460 |
|
| 461 |
/* Fold by 4. */ |
| 462 |
while (inlen >= 4 * 16) |
| 463 |
{ |
| 464 |
v0 = CRC_VEC_U64_LOAD_BE(0 * 16, inbuf); |
| 465 |
crc0 = asm_vpmsumd(crc0, k2k1) ^ v0; |
| 466 |
|
| 467 |
v0 = CRC_VEC_U64_LOAD_BE(1 * 16, inbuf); |
| 468 |
crc1 = asm_vpmsumd(crc1, k2k1) ^ v0; |
| 469 |
|
| 470 |
v0 = CRC_VEC_U64_LOAD_BE(2 * 16, inbuf); |
| 471 |
crc2 = asm_vpmsumd(crc2, k2k1) ^ v0; |
| 472 |
|
| 473 |
v0 = CRC_VEC_U64_LOAD_BE(3 * 16, inbuf); |
| 474 |
crc3 = asm_vpmsumd(crc3, k2k1) ^ v0; |
| 475 |
|
| 476 |
inbuf += 4 * 16; |
| 477 |
inlen -= 4 * 16; |
| 478 |
} |
| 479 |
|
| 480 |
/* Fold 4 to 1. */ |
| 481 |
crc1 ^= asm_vpmsumd(crc0, k4k3); |
| 482 |
crc2 ^= asm_vpmsumd(crc1, k4k3); |
| 483 |
crc3 ^= asm_vpmsumd(crc2, k4k3); |
| 484 |
crc = crc3; |
| 485 |
} |
| 486 |
else |
| 487 |
{ |
| 488 |
v0 = CRC_VEC_U64_LOAD_BE(0, inbuf); |
| 489 |
crc ^= v0; |
| 490 |
|
| 491 |
inbuf += 16; |
| 492 |
inlen -= 16; |
| 493 |
} |
| 494 |
|
| 495 |
/* Fold by 1. */ |
| 496 |
while (inlen >= 16) |
| 497 |
{ |
| 498 |
v0 = CRC_VEC_U64_LOAD_BE(0, inbuf); |
| 499 |
crc = asm_vpmsumd(k4k3, crc); |
| 500 |
crc ^= v0; |
| 501 |
|
| 502 |
inbuf += 16; |
| 503 |
inlen -= 16; |
| 504 |
} |
| 505 |
|
| 506 |
/* Partial fold. */ |
| 507 |
if (inlen) |
| 508 |
{ |
| 509 |
/* Load last input and add padding zeros. */ |
| 510 |
vector2x_u64 mask = CRC_VEC_U64_LOAD_LE(inlen, crc32_partial_fold_input_mask); |
| 511 |
vector2x_u64 shl_shuf = CRC_VEC_U64_LOAD_LE(32 - inlen, crc32_refl_shuf_shift); |
| 512 |
vector2x_u64 shr_shuf = CRC_VEC_U64_LOAD_LE(inlen + 16, crc32_shuf_shift); |
| 513 |
|
| 514 |
v0 = CRC_VEC_U64_LOAD_LE(inlen - 16, inbuf); |
| 515 |
v0 &= mask; |
| 516 |
|
| 517 |
crc = CRC_VEC_SWAP_TO_LE(crc); |
| 518 |
crc2 = (vector2x_u64)vec_perm((vector16x_u8)crc, (vector16x_u8)zero, |
| 519 |
(vector16x_u8)shr_shuf); |
| 520 |
v0 |= crc2; |
| 521 |
v0 = CRC_VEC_SWAP(v0); |
| 522 |
crc = (vector2x_u64)vec_perm((vector16x_u8)crc, (vector16x_u8)zero, |
| 523 |
(vector16x_u8)shl_shuf); |
| 524 |
crc = asm_vpmsumd(k4k3, crc); |
| 525 |
crc ^= v0; |
| 526 |
|
| 527 |
inbuf += inlen; |
| 528 |
inlen -= inlen; |
| 529 |
} |
| 530 |
|
| 531 |
/* Final fold. */ |
| 532 |
|
| 533 |
/* reduce 128-bits to 96-bits */ |
| 534 |
v0 = (vector2x_u64)vec_sld_u32((vector4x_u32)crc, |
| 535 |
(vector4x_u32)zero, 2); |
| 536 |
crc = asm_vpmsumd(k4hi, crc); |
| 537 |
crc ^= v0; /* bottom 32-bit are zero */ |
| 538 |
|
| 539 |
/* reduce 96-bits to 64-bits */ |
| 540 |
v0 = crc & low_96bit_mask; /* [00][x2][x1][00] */ |
| 541 |
crc >>= 32; /* [00][x3][00][x0] */ |
| 542 |
crc = asm_vpmsumd(k5hi, crc); /* [00][xx][xx][00] */ |
| 543 |
crc ^= v0; /* top and bottom 32-bit are zero */ |
| 544 |
|
| 545 |
/* barrett reduction */ |
| 546 |
p_my_hi = p_my; |
| 547 |
p_my_lo = p_my; |
| 548 |
p_my_hi[VEC_U64_LO] = 0; |
| 549 |
p_my_lo[VEC_U64_HI] = 0; |
| 550 |
v0 = crc >> 32; /* [00][00][00][x1] */ |
| 551 |
crc = asm_vpmsumd(p_my_hi, crc); /* [00][xx][xx][xx] */ |
| 552 |
crc = (vector2x_u64)vec_sld_u32((vector4x_u32)crc, |
| 553 |
(vector4x_u32)crc, 3); /* [x0][00][x2][x1] */ |
| 554 |
crc = asm_vpmsumd(p_my_lo, crc); /* [00][xx][xx][xx] */ |
| 555 |
crc ^= v0; |
| 556 |
|
| 557 |
*pcrc = _gcry_bswap32(crc[VEC_U64_LO]); |
| 558 |
} |
| 559 |
|
| 560 |
|
| 561 |
static ASM_FUNC_ATTR_INLINE u32 |
| 562 |
crc32_ppc8_ce_reduction_4 (u32 data, u32 crc, |
| 563 |
const struct crc32_consts_s *consts) |
| 564 |
{ |
| 565 |
vector2x_u64 my_p = CRC_VEC_U64_LOAD(0, &consts->my_p[0]); |
| 566 |
vector2x_u64 v0 = CRC_VEC_U64_DEF((u64)data << 32, 0); |
| 567 |
v0 = asm_vpmsumd(v0, my_p); /* [00][x1][x0][00] */ |
| 568 |
v0[VEC_U64_LO] = 0; /* [00][x1][00][00] */ |
| 569 |
v0 = asm_vpmsumd(v0, my_p); /* [00][00][xx][xx] */ |
| 570 |
return _gcry_bswap32(v0[VEC_U64_LO]) ^ crc; |
| 571 |
} |
| 572 |
|
| 573 |
|
| 574 |
static ASM_FUNC_ATTR_INLINE void |
| 575 |
crc32_less_than_16 (u32 *pcrc, const byte *inbuf, size_t inlen, |
| 576 |
const struct crc32_consts_s *consts) |
| 577 |
{ |
| 578 |
u32 crc = *pcrc; |
| 579 |
u32 data; |
| 580 |
|
| 581 |
while (inlen >= 4) |
| 582 |
{ |
| 583 |
data = buf_get_le32(inbuf); |
| 584 |
data ^= crc; |
| 585 |
data = _gcry_bswap32(data); |
| 586 |
|
| 587 |
inlen -= 4; |
| 588 |
inbuf += 4; |
| 589 |
|
| 590 |
crc = crc32_ppc8_ce_reduction_4 (data, 0, consts); |
| 591 |
} |
| 592 |
|
| 593 |
switch (inlen) |
| 594 |
{ |
| 595 |
case 0: |
| 596 |
break; |
| 597 |
case 1: |
| 598 |
data = inbuf[0]; |
| 599 |
data ^= crc; |
| 600 |
data = data & 0xffU; |
| 601 |
crc = crc >> 8; |
| 602 |
crc = crc32_ppc8_ce_reduction_4 (data, crc, consts); |
| 603 |
break; |
| 604 |
case 2: |
| 605 |
data = inbuf[0] << 0; |
| 606 |
data |= inbuf[1] << 8; |
| 607 |
data ^= crc; |
| 608 |
data = _gcry_bswap32(data << 16); |
| 609 |
crc = crc >> 16; |
| 610 |
crc = crc32_ppc8_ce_reduction_4 (data, crc, consts); |
| 611 |
break; |
| 612 |
case 3: |
| 613 |
data = inbuf[0] << 0; |
| 614 |
data |= inbuf[1] << 8; |
| 615 |
data |= inbuf[2] << 16; |
| 616 |
data ^= crc; |
| 617 |
data = _gcry_bswap32(data << 8); |
| 618 |
crc = crc >> 24; |
| 619 |
crc = crc32_ppc8_ce_reduction_4 (data, crc, consts); |
| 620 |
break; |
| 621 |
} |
| 622 |
|
| 623 |
*pcrc = crc; |
| 624 |
} |
| 625 |
|
| 626 |
void ASM_FUNC_ATTR |
| 627 |
_gcry_crc32_ppc8_vpmsum (u32 *pcrc, const byte *inbuf, size_t inlen) |
| 628 |
{ |
| 629 |
const struct crc32_consts_s *consts = &crc32_consts; |
| 630 |
|
| 631 |
if (!inlen) |
| 632 |
return; |
| 633 |
|
| 634 |
if (inlen >= 16) |
| 635 |
crc32r_ppc8_ce_bulk (pcrc, inbuf, inlen, consts); |
| 636 |
else |
| 637 |
crc32r_less_than_16 (pcrc, inbuf, inlen, consts); |
| 638 |
} |
| 639 |
|
| 640 |
void ASM_FUNC_ATTR |
| 641 |
_gcry_crc24rfc2440_ppc8_vpmsum (u32 *pcrc, const byte *inbuf, size_t inlen) |
| 642 |
{ |
| 643 |
const struct crc32_consts_s *consts = &crc24rfc2440_consts; |
| 644 |
|
| 645 |
if (!inlen) |
| 646 |
return; |
| 647 |
|
| 648 |
/* Note: *pcrc in input endian. */ |
| 649 |
|
| 650 |
if (inlen >= 16) |
| 651 |
crc32_ppc8_ce_bulk (pcrc, inbuf, inlen, consts); |
| 652 |
else |
| 653 |
crc32_less_than_16 (pcrc, inbuf, inlen, consts); |
| 654 |
} |
| 655 |
|
| 656 |
#endif |