xref: /freebsd-src/sys/contrib/openzfs/module/zcommon/zfs_fletcher_intel.c (revision 2a58b312b62f908ec92311d1bd8536dbaeb8e55b)
1eda14cbcSMatt Macy /*
2eda14cbcSMatt Macy  * Implement fast Fletcher4 with AVX2 instructions. (x86_64)
3eda14cbcSMatt Macy  *
4eda14cbcSMatt Macy  * Use the 256-bit AVX2 SIMD instructions and registers to compute
5eda14cbcSMatt Macy  * Fletcher4 in four incremental 64-bit parallel accumulator streams,
6eda14cbcSMatt Macy  * and then combine the streams to form the final four checksum words.
7eda14cbcSMatt Macy  *
8eda14cbcSMatt Macy  * Copyright (C) 2015 Intel Corporation.
9eda14cbcSMatt Macy  *
10eda14cbcSMatt Macy  * Authors:
11eda14cbcSMatt Macy  *      James Guilford <james.guilford@intel.com>
12eda14cbcSMatt Macy  *      Jinshan Xiong <jinshan.xiong@intel.com>
13eda14cbcSMatt Macy  *
14eda14cbcSMatt Macy  * This software is available to you under a choice of one of two
15eda14cbcSMatt Macy  * licenses.  You may choose to be licensed under the terms of the GNU
16eda14cbcSMatt Macy  * General Public License (GPL) Version 2, available from the file
17eda14cbcSMatt Macy  * COPYING in the main directory of this source tree, or the
18eda14cbcSMatt Macy  * OpenIB.org BSD license below:
19eda14cbcSMatt Macy  *
20eda14cbcSMatt Macy  *     Redistribution and use in source and binary forms, with or
21eda14cbcSMatt Macy  *     without modification, are permitted provided that the following
22eda14cbcSMatt Macy  *     conditions are met:
23eda14cbcSMatt Macy  *
24eda14cbcSMatt Macy  *      - Redistributions of source code must retain the above
25eda14cbcSMatt Macy  *        copyright notice, this list of conditions and the following
26eda14cbcSMatt Macy  *        disclaimer.
27eda14cbcSMatt Macy  *
28eda14cbcSMatt Macy  *      - Redistributions in binary form must reproduce the above
29eda14cbcSMatt Macy  *        copyright notice, this list of conditions and the following
30eda14cbcSMatt Macy  *        disclaimer in the documentation and/or other materials
31eda14cbcSMatt Macy  *        provided with the distribution.
32eda14cbcSMatt Macy  *
33eda14cbcSMatt Macy  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
34eda14cbcSMatt Macy  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
35eda14cbcSMatt Macy  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
36eda14cbcSMatt Macy  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
37eda14cbcSMatt Macy  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
38eda14cbcSMatt Macy  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
39eda14cbcSMatt Macy  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
40eda14cbcSMatt Macy  * SOFTWARE.
41eda14cbcSMatt Macy  */
42eda14cbcSMatt Macy 
43eda14cbcSMatt Macy #if defined(HAVE_AVX) && defined(HAVE_AVX2)
44eda14cbcSMatt Macy 
45eda14cbcSMatt Macy #include <sys/spa_checksum.h>
46da5137abSMartin Matuska #include <sys/string.h>
47eda14cbcSMatt Macy #include <sys/simd.h>
48eda14cbcSMatt Macy #include <zfs_fletcher.h>
49eda14cbcSMatt Macy 
50eda14cbcSMatt Macy static void
fletcher_4_avx2_init(fletcher_4_ctx_t * ctx)51eda14cbcSMatt Macy fletcher_4_avx2_init(fletcher_4_ctx_t *ctx)
52eda14cbcSMatt Macy {
53da5137abSMartin Matuska 	memset(ctx->avx, 0, 4 * sizeof (zfs_fletcher_avx_t));
54eda14cbcSMatt Macy }
55eda14cbcSMatt Macy 
56eda14cbcSMatt Macy static void
fletcher_4_avx2_fini(fletcher_4_ctx_t * ctx,zio_cksum_t * zcp)57eda14cbcSMatt Macy fletcher_4_avx2_fini(fletcher_4_ctx_t *ctx, zio_cksum_t *zcp)
58eda14cbcSMatt Macy {
59eda14cbcSMatt Macy 	uint64_t A, B, C, D;
60eda14cbcSMatt Macy 
61eda14cbcSMatt Macy 	A = ctx->avx[0].v[0] + ctx->avx[0].v[1] +
62eda14cbcSMatt Macy 	    ctx->avx[0].v[2] + ctx->avx[0].v[3];
63eda14cbcSMatt Macy 	B = 0 - ctx->avx[0].v[1] - 2 * ctx->avx[0].v[2] - 3 * ctx->avx[0].v[3] +
64eda14cbcSMatt Macy 	    4 * ctx->avx[1].v[0] + 4 * ctx->avx[1].v[1] + 4 * ctx->avx[1].v[2] +
65eda14cbcSMatt Macy 	    4 * ctx->avx[1].v[3];
66eda14cbcSMatt Macy 
67eda14cbcSMatt Macy 	C = ctx->avx[0].v[2] + 3 * ctx->avx[0].v[3] - 6 * ctx->avx[1].v[0] -
68eda14cbcSMatt Macy 	    10 * ctx->avx[1].v[1] - 14 * ctx->avx[1].v[2] -
69eda14cbcSMatt Macy 	    18 * ctx->avx[1].v[3] + 16 * ctx->avx[2].v[0] +
70eda14cbcSMatt Macy 	    16 * ctx->avx[2].v[1] + 16 * ctx->avx[2].v[2] +
71eda14cbcSMatt Macy 	    16 * ctx->avx[2].v[3];
72eda14cbcSMatt Macy 
73eda14cbcSMatt Macy 	D = 0 - ctx->avx[0].v[3] + 4 * ctx->avx[1].v[0] +
74eda14cbcSMatt Macy 	    10 * ctx->avx[1].v[1] + 20 * ctx->avx[1].v[2] +
75eda14cbcSMatt Macy 	    34 * ctx->avx[1].v[3] - 48 * ctx->avx[2].v[0] -
76eda14cbcSMatt Macy 	    64 * ctx->avx[2].v[1] - 80 * ctx->avx[2].v[2] -
77eda14cbcSMatt Macy 	    96 * ctx->avx[2].v[3] + 64 * ctx->avx[3].v[0] +
78eda14cbcSMatt Macy 	    64 * ctx->avx[3].v[1] + 64 * ctx->avx[3].v[2] +
79eda14cbcSMatt Macy 	    64 * ctx->avx[3].v[3];
80eda14cbcSMatt Macy 
81eda14cbcSMatt Macy 	ZIO_SET_CHECKSUM(zcp, A, B, C, D);
82eda14cbcSMatt Macy }
83eda14cbcSMatt Macy 
84eda14cbcSMatt Macy #define	FLETCHER_4_AVX2_RESTORE_CTX(ctx)				\
85eda14cbcSMatt Macy {									\
86eda14cbcSMatt Macy 	asm volatile("vmovdqu %0, %%ymm0" :: "m" ((ctx)->avx[0]));	\
87eda14cbcSMatt Macy 	asm volatile("vmovdqu %0, %%ymm1" :: "m" ((ctx)->avx[1]));	\
88eda14cbcSMatt Macy 	asm volatile("vmovdqu %0, %%ymm2" :: "m" ((ctx)->avx[2]));	\
89eda14cbcSMatt Macy 	asm volatile("vmovdqu %0, %%ymm3" :: "m" ((ctx)->avx[3]));	\
90eda14cbcSMatt Macy }
91eda14cbcSMatt Macy 
92eda14cbcSMatt Macy #define	FLETCHER_4_AVX2_SAVE_CTX(ctx)					\
93eda14cbcSMatt Macy {									\
94eda14cbcSMatt Macy 	asm volatile("vmovdqu %%ymm0, %0" : "=m" ((ctx)->avx[0]));	\
95eda14cbcSMatt Macy 	asm volatile("vmovdqu %%ymm1, %0" : "=m" ((ctx)->avx[1]));	\
96eda14cbcSMatt Macy 	asm volatile("vmovdqu %%ymm2, %0" : "=m" ((ctx)->avx[2]));	\
97eda14cbcSMatt Macy 	asm volatile("vmovdqu %%ymm3, %0" : "=m" ((ctx)->avx[3]));	\
98eda14cbcSMatt Macy }
99eda14cbcSMatt Macy 
100eda14cbcSMatt Macy 
101eda14cbcSMatt Macy static void
fletcher_4_avx2_native(fletcher_4_ctx_t * ctx,const void * buf,uint64_t size)102eda14cbcSMatt Macy fletcher_4_avx2_native(fletcher_4_ctx_t *ctx, const void *buf, uint64_t size)
103eda14cbcSMatt Macy {
104eda14cbcSMatt Macy 	const uint64_t *ip = buf;
105eda14cbcSMatt Macy 	const uint64_t *ipend = (uint64_t *)((uint8_t *)ip + size);
106eda14cbcSMatt Macy 
107eda14cbcSMatt Macy 	FLETCHER_4_AVX2_RESTORE_CTX(ctx);
108eda14cbcSMatt Macy 
109bb2d13b6SMartin Matuska 	do {
110eda14cbcSMatt Macy 		asm volatile("vpmovzxdq %0, %%ymm4"::"m" (*ip));
111eda14cbcSMatt Macy 		asm volatile("vpaddq %ymm4, %ymm0, %ymm0");
112eda14cbcSMatt Macy 		asm volatile("vpaddq %ymm0, %ymm1, %ymm1");
113eda14cbcSMatt Macy 		asm volatile("vpaddq %ymm1, %ymm2, %ymm2");
114eda14cbcSMatt Macy 		asm volatile("vpaddq %ymm2, %ymm3, %ymm3");
115bb2d13b6SMartin Matuska 	} while ((ip += 2) < ipend);
116eda14cbcSMatt Macy 
117eda14cbcSMatt Macy 	FLETCHER_4_AVX2_SAVE_CTX(ctx);
118eda14cbcSMatt Macy 	asm volatile("vzeroupper");
119eda14cbcSMatt Macy }
120eda14cbcSMatt Macy 
121eda14cbcSMatt Macy static void
fletcher_4_avx2_byteswap(fletcher_4_ctx_t * ctx,const void * buf,uint64_t size)122eda14cbcSMatt Macy fletcher_4_avx2_byteswap(fletcher_4_ctx_t *ctx, const void *buf, uint64_t size)
123eda14cbcSMatt Macy {
124eda14cbcSMatt Macy 	static const zfs_fletcher_avx_t mask = {
125eda14cbcSMatt Macy 		.v = { 0xFFFFFFFF00010203, 0xFFFFFFFF08090A0B,
126eda14cbcSMatt Macy 		    0xFFFFFFFF00010203, 0xFFFFFFFF08090A0B }
127eda14cbcSMatt Macy 	};
128eda14cbcSMatt Macy 	const uint64_t *ip = buf;
129eda14cbcSMatt Macy 	const uint64_t *ipend = (uint64_t *)((uint8_t *)ip + size);
130eda14cbcSMatt Macy 
131eda14cbcSMatt Macy 	FLETCHER_4_AVX2_RESTORE_CTX(ctx);
132eda14cbcSMatt Macy 
133eda14cbcSMatt Macy 	asm volatile("vmovdqu %0, %%ymm5" :: "m" (mask));
134eda14cbcSMatt Macy 
135bb2d13b6SMartin Matuska 	do {
136eda14cbcSMatt Macy 		asm volatile("vpmovzxdq %0, %%ymm4"::"m" (*ip));
137eda14cbcSMatt Macy 		asm volatile("vpshufb %ymm5, %ymm4, %ymm4");
138eda14cbcSMatt Macy 
139eda14cbcSMatt Macy 		asm volatile("vpaddq %ymm4, %ymm0, %ymm0");
140eda14cbcSMatt Macy 		asm volatile("vpaddq %ymm0, %ymm1, %ymm1");
141eda14cbcSMatt Macy 		asm volatile("vpaddq %ymm1, %ymm2, %ymm2");
142eda14cbcSMatt Macy 		asm volatile("vpaddq %ymm2, %ymm3, %ymm3");
143bb2d13b6SMartin Matuska 	} while ((ip += 2) < ipend);
144eda14cbcSMatt Macy 
145eda14cbcSMatt Macy 	FLETCHER_4_AVX2_SAVE_CTX(ctx);
146eda14cbcSMatt Macy 	asm volatile("vzeroupper");
147eda14cbcSMatt Macy }
148eda14cbcSMatt Macy 
fletcher_4_avx2_valid(void)149eda14cbcSMatt Macy static boolean_t fletcher_4_avx2_valid(void)
150eda14cbcSMatt Macy {
151eda14cbcSMatt Macy 	return (kfpu_allowed() && zfs_avx_available() && zfs_avx2_available());
152eda14cbcSMatt Macy }
153eda14cbcSMatt Macy 
154eda14cbcSMatt Macy const fletcher_4_ops_t fletcher_4_avx2_ops = {
155eda14cbcSMatt Macy 	.init_native = fletcher_4_avx2_init,
156eda14cbcSMatt Macy 	.fini_native = fletcher_4_avx2_fini,
157eda14cbcSMatt Macy 	.compute_native = fletcher_4_avx2_native,
158eda14cbcSMatt Macy 	.init_byteswap = fletcher_4_avx2_init,
159eda14cbcSMatt Macy 	.fini_byteswap = fletcher_4_avx2_fini,
160eda14cbcSMatt Macy 	.compute_byteswap = fletcher_4_avx2_byteswap,
161eda14cbcSMatt Macy 	.valid = fletcher_4_avx2_valid,
162*2a58b312SMartin Matuska 	.uses_fpu = B_TRUE,
163eda14cbcSMatt Macy 	.name = "avx2"
164eda14cbcSMatt Macy };
165eda14cbcSMatt Macy 
166eda14cbcSMatt Macy #endif /* defined(HAVE_AVX) && defined(HAVE_AVX2) */
167