xref: /linux/lib/crc/crc64-neon.c (revision 0eaed89c18aeedf0898baf2dbf5ff027c6795152)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Accelerated CRC64 (NVMe) using ARM NEON C intrinsics
4  */
5 
6 #include <linux/types.h>
7 #include <asm/neon-intrinsics.h>
8 
9 #include "crc64-neon.h"
10 
11 u64 crc64_nvme_neon(u64 crc, const u8 *p, size_t len);
12 
13 /* x^191 mod G, x^127 mod G */
14 static const u64 fold_consts_val[2] = { 0xeadc41fd2ba3d420ULL,
15 					0x21e9761e252621acULL };
16 /* floor(x^127 / G), (G - x^64) / x */
17 static const u64 bconsts_val[2] = { 0x27ecfa329aef9f77ULL,
18 				    0x34d926535897936aULL };
19 
20 u64 crc64_nvme_neon(u64 crc, const u8 *p, size_t len)
21 {
22 	uint64x2_t fold_consts = vld1q_u64(fold_consts_val);
23 	uint64x2_t v0 = { crc, 0 };
24 	uint64x2_t zero = { };
25 
26 	for (;;) {
27 		v0 ^= vreinterpretq_u64_u8(vld1q_u8(p));
28 
29 		p += 16;
30 		len -= 16;
31 		if (len < 16)
32 			break;
33 
34 		v0 = pmull64(fold_consts, v0) ^ pmull64_high(fold_consts, v0);
35 	}
36 
37 	/* Multiply the 128-bit value by x^64 and reduce it back to 128 bits. */
38 	v0 = vextq_u64(v0, zero, 1) ^ pmull64_hi_lo(fold_consts, v0);
39 
40 	/* Final Barrett reduction */
41 	uint64x2_t bconsts = vld1q_u64(bconsts_val);
42 	uint64x2_t final = pmull64(bconsts, v0);
43 
44 	v0 ^= vextq_u64(zero, final, 1) ^ pmull64_hi_lo(bconsts, final);
45 
46 	return vgetq_lane_u64(v0, 1);
47 }
48