1eda14cbcSMatt Macy /*
2eda14cbcSMatt Macy * CDDL HEADER START
3eda14cbcSMatt Macy *
4eda14cbcSMatt Macy * The contents of this file are subject to the terms of the
5eda14cbcSMatt Macy * Common Development and Distribution License (the "License").
6eda14cbcSMatt Macy * You may not use this file except in compliance with the License.
7eda14cbcSMatt Macy *
8eda14cbcSMatt Macy * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9271171e0SMartin Matuska * or https://opensource.org/licenses/CDDL-1.0.
10eda14cbcSMatt Macy * See the License for the specific language governing permissions
11eda14cbcSMatt Macy * and limitations under the License.
12eda14cbcSMatt Macy *
13eda14cbcSMatt Macy * When distributing Covered Code, include this CDDL HEADER in each
14eda14cbcSMatt Macy * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15eda14cbcSMatt Macy * If applicable, add the following below this CDDL HEADER, with the
16eda14cbcSMatt Macy * fields enclosed by brackets "[]" replaced with your own identifying
17eda14cbcSMatt Macy * information: Portions Copyright [yyyy] [name of copyright owner]
18eda14cbcSMatt Macy *
19eda14cbcSMatt Macy * CDDL HEADER END
20eda14cbcSMatt Macy */
21eda14cbcSMatt Macy /*
22eda14cbcSMatt Macy * Copyright (C) 2016 Gvozden Nešković. All rights reserved.
23eda14cbcSMatt Macy */
24eda14cbcSMatt Macy
25eda14cbcSMatt Macy #if defined(__x86_64) && defined(HAVE_AVX512F)
26eda14cbcSMatt Macy
27eda14cbcSMatt Macy #include <sys/byteorder.h>
28eda14cbcSMatt Macy #include <sys/frame.h>
29eda14cbcSMatt Macy #include <sys/spa_checksum.h>
30da5137abSMartin Matuska #include <sys/string.h>
31eda14cbcSMatt Macy #include <sys/simd.h>
32eda14cbcSMatt Macy #include <zfs_fletcher.h>
33eda14cbcSMatt Macy
34eda14cbcSMatt Macy #ifdef __linux__
35eda14cbcSMatt Macy #define __asm __asm__ __volatile__
36eda14cbcSMatt Macy #endif
37eda14cbcSMatt Macy
38eda14cbcSMatt Macy static void
fletcher_4_avx512f_init(fletcher_4_ctx_t * ctx)39eda14cbcSMatt Macy fletcher_4_avx512f_init(fletcher_4_ctx_t *ctx)
40eda14cbcSMatt Macy {
41da5137abSMartin Matuska memset(ctx->avx512, 0, 4 * sizeof (zfs_fletcher_avx512_t));
42eda14cbcSMatt Macy }
43eda14cbcSMatt Macy
44eda14cbcSMatt Macy static void
fletcher_4_avx512f_fini(fletcher_4_ctx_t * ctx,zio_cksum_t * zcp)45eda14cbcSMatt Macy fletcher_4_avx512f_fini(fletcher_4_ctx_t *ctx, zio_cksum_t *zcp)
46eda14cbcSMatt Macy {
47eda14cbcSMatt Macy static const uint64_t
48eda14cbcSMatt Macy CcA[] = { 0, 0, 1, 3, 6, 10, 15, 21 },
49eda14cbcSMatt Macy CcB[] = { 28, 36, 44, 52, 60, 68, 76, 84 },
50eda14cbcSMatt Macy DcA[] = { 0, 0, 0, 1, 4, 10, 20, 35 },
51eda14cbcSMatt Macy DcB[] = { 56, 84, 120, 164, 216, 276, 344, 420 },
52eda14cbcSMatt Macy DcC[] = { 448, 512, 576, 640, 704, 768, 832, 896 };
53eda14cbcSMatt Macy
54eda14cbcSMatt Macy uint64_t A, B, C, D;
55eda14cbcSMatt Macy uint64_t i;
56eda14cbcSMatt Macy
57eda14cbcSMatt Macy A = ctx->avx512[0].v[0];
58eda14cbcSMatt Macy B = 8 * ctx->avx512[1].v[0];
59eda14cbcSMatt Macy C = 64 * ctx->avx512[2].v[0] - CcB[0] * ctx->avx512[1].v[0];
60eda14cbcSMatt Macy D = 512 * ctx->avx512[3].v[0] - DcC[0] * ctx->avx512[2].v[0] +
61eda14cbcSMatt Macy DcB[0] * ctx->avx512[1].v[0];
62eda14cbcSMatt Macy
63eda14cbcSMatt Macy for (i = 1; i < 8; i++) {
64eda14cbcSMatt Macy A += ctx->avx512[0].v[i];
65eda14cbcSMatt Macy B += 8 * ctx->avx512[1].v[i] - i * ctx->avx512[0].v[i];
66eda14cbcSMatt Macy C += 64 * ctx->avx512[2].v[i] - CcB[i] * ctx->avx512[1].v[i] +
67eda14cbcSMatt Macy CcA[i] * ctx->avx512[0].v[i];
68eda14cbcSMatt Macy D += 512 * ctx->avx512[3].v[i] - DcC[i] * ctx->avx512[2].v[i] +
69eda14cbcSMatt Macy DcB[i] * ctx->avx512[1].v[i] - DcA[i] * ctx->avx512[0].v[i];
70eda14cbcSMatt Macy }
71eda14cbcSMatt Macy
72eda14cbcSMatt Macy ZIO_SET_CHECKSUM(zcp, A, B, C, D);
73eda14cbcSMatt Macy }
74eda14cbcSMatt Macy
75eda14cbcSMatt Macy #define FLETCHER_4_AVX512_RESTORE_CTX(ctx) \
76eda14cbcSMatt Macy { \
77eda14cbcSMatt Macy __asm("vmovdqu64 %0, %%zmm0" :: "m" ((ctx)->avx512[0])); \
78eda14cbcSMatt Macy __asm("vmovdqu64 %0, %%zmm1" :: "m" ((ctx)->avx512[1])); \
79eda14cbcSMatt Macy __asm("vmovdqu64 %0, %%zmm2" :: "m" ((ctx)->avx512[2])); \
80eda14cbcSMatt Macy __asm("vmovdqu64 %0, %%zmm3" :: "m" ((ctx)->avx512[3])); \
81eda14cbcSMatt Macy }
82eda14cbcSMatt Macy
83eda14cbcSMatt Macy #define FLETCHER_4_AVX512_SAVE_CTX(ctx) \
84eda14cbcSMatt Macy { \
85eda14cbcSMatt Macy __asm("vmovdqu64 %%zmm0, %0" : "=m" ((ctx)->avx512[0])); \
86eda14cbcSMatt Macy __asm("vmovdqu64 %%zmm1, %0" : "=m" ((ctx)->avx512[1])); \
87eda14cbcSMatt Macy __asm("vmovdqu64 %%zmm2, %0" : "=m" ((ctx)->avx512[2])); \
88eda14cbcSMatt Macy __asm("vmovdqu64 %%zmm3, %0" : "=m" ((ctx)->avx512[3])); \
89eda14cbcSMatt Macy }
90eda14cbcSMatt Macy
91eda14cbcSMatt Macy static void
fletcher_4_avx512f_native(fletcher_4_ctx_t * ctx,const void * buf,uint64_t size)92eda14cbcSMatt Macy fletcher_4_avx512f_native(fletcher_4_ctx_t *ctx, const void *buf, uint64_t size)
93eda14cbcSMatt Macy {
94eda14cbcSMatt Macy const uint32_t *ip = buf;
95eda14cbcSMatt Macy const uint32_t *ipend = (uint32_t *)((uint8_t *)ip + size);
96eda14cbcSMatt Macy
97eda14cbcSMatt Macy FLETCHER_4_AVX512_RESTORE_CTX(ctx);
98eda14cbcSMatt Macy
99bb2d13b6SMartin Matuska do {
100eda14cbcSMatt Macy __asm("vpmovzxdq %0, %%zmm4"::"m" (*ip));
101eda14cbcSMatt Macy __asm("vpaddq %zmm4, %zmm0, %zmm0");
102eda14cbcSMatt Macy __asm("vpaddq %zmm0, %zmm1, %zmm1");
103eda14cbcSMatt Macy __asm("vpaddq %zmm1, %zmm2, %zmm2");
104eda14cbcSMatt Macy __asm("vpaddq %zmm2, %zmm3, %zmm3");
105bb2d13b6SMartin Matuska } while ((ip += 8) < ipend);
106eda14cbcSMatt Macy
107eda14cbcSMatt Macy FLETCHER_4_AVX512_SAVE_CTX(ctx);
108eda14cbcSMatt Macy }
109eda14cbcSMatt Macy STACK_FRAME_NON_STANDARD(fletcher_4_avx512f_native);
110eda14cbcSMatt Macy
111eda14cbcSMatt Macy static void
fletcher_4_avx512f_byteswap(fletcher_4_ctx_t * ctx,const void * buf,uint64_t size)112eda14cbcSMatt Macy fletcher_4_avx512f_byteswap(fletcher_4_ctx_t *ctx, const void *buf,
113eda14cbcSMatt Macy uint64_t size)
114eda14cbcSMatt Macy {
115eda14cbcSMatt Macy static const uint64_t byteswap_mask = 0xFFULL;
116eda14cbcSMatt Macy const uint32_t *ip = buf;
117eda14cbcSMatt Macy const uint32_t *ipend = (uint32_t *)((uint8_t *)ip + size);
118eda14cbcSMatt Macy
119eda14cbcSMatt Macy FLETCHER_4_AVX512_RESTORE_CTX(ctx);
120eda14cbcSMatt Macy
121eda14cbcSMatt Macy __asm("vpbroadcastq %0, %%zmm8" :: "r" (byteswap_mask));
122eda14cbcSMatt Macy __asm("vpsllq $8, %zmm8, %zmm9");
123eda14cbcSMatt Macy __asm("vpsllq $16, %zmm8, %zmm10");
124eda14cbcSMatt Macy __asm("vpsllq $24, %zmm8, %zmm11");
125eda14cbcSMatt Macy
126bb2d13b6SMartin Matuska do {
127eda14cbcSMatt Macy __asm("vpmovzxdq %0, %%zmm5"::"m" (*ip));
128eda14cbcSMatt Macy
129eda14cbcSMatt Macy __asm("vpsrlq $24, %zmm5, %zmm6");
130eda14cbcSMatt Macy __asm("vpandd %zmm8, %zmm6, %zmm6");
131eda14cbcSMatt Macy __asm("vpsrlq $8, %zmm5, %zmm7");
132eda14cbcSMatt Macy __asm("vpandd %zmm9, %zmm7, %zmm7");
133eda14cbcSMatt Macy __asm("vpord %zmm6, %zmm7, %zmm4");
134eda14cbcSMatt Macy __asm("vpsllq $8, %zmm5, %zmm6");
135eda14cbcSMatt Macy __asm("vpandd %zmm10, %zmm6, %zmm6");
136eda14cbcSMatt Macy __asm("vpord %zmm6, %zmm4, %zmm4");
137eda14cbcSMatt Macy __asm("vpsllq $24, %zmm5, %zmm5");
138eda14cbcSMatt Macy __asm("vpandd %zmm11, %zmm5, %zmm5");
139eda14cbcSMatt Macy __asm("vpord %zmm5, %zmm4, %zmm4");
140eda14cbcSMatt Macy
141eda14cbcSMatt Macy __asm("vpaddq %zmm4, %zmm0, %zmm0");
142eda14cbcSMatt Macy __asm("vpaddq %zmm0, %zmm1, %zmm1");
143eda14cbcSMatt Macy __asm("vpaddq %zmm1, %zmm2, %zmm2");
144eda14cbcSMatt Macy __asm("vpaddq %zmm2, %zmm3, %zmm3");
145bb2d13b6SMartin Matuska } while ((ip += 8) < ipend);
146eda14cbcSMatt Macy
147eda14cbcSMatt Macy FLETCHER_4_AVX512_SAVE_CTX(ctx)
148eda14cbcSMatt Macy }
149eda14cbcSMatt Macy STACK_FRAME_NON_STANDARD(fletcher_4_avx512f_byteswap);
150eda14cbcSMatt Macy
151eda14cbcSMatt Macy static boolean_t
fletcher_4_avx512f_valid(void)152eda14cbcSMatt Macy fletcher_4_avx512f_valid(void)
153eda14cbcSMatt Macy {
154eda14cbcSMatt Macy return (kfpu_allowed() && zfs_avx512f_available());
155eda14cbcSMatt Macy }
156eda14cbcSMatt Macy
157eda14cbcSMatt Macy const fletcher_4_ops_t fletcher_4_avx512f_ops = {
158eda14cbcSMatt Macy .init_native = fletcher_4_avx512f_init,
159eda14cbcSMatt Macy .fini_native = fletcher_4_avx512f_fini,
160eda14cbcSMatt Macy .compute_native = fletcher_4_avx512f_native,
161eda14cbcSMatt Macy .init_byteswap = fletcher_4_avx512f_init,
162eda14cbcSMatt Macy .fini_byteswap = fletcher_4_avx512f_fini,
163eda14cbcSMatt Macy .compute_byteswap = fletcher_4_avx512f_byteswap,
164eda14cbcSMatt Macy .valid = fletcher_4_avx512f_valid,
165*2a58b312SMartin Matuska .uses_fpu = B_TRUE,
166eda14cbcSMatt Macy .name = "avx512f"
167eda14cbcSMatt Macy };
168eda14cbcSMatt Macy
169eda14cbcSMatt Macy #if defined(HAVE_AVX512BW)
170eda14cbcSMatt Macy static void
fletcher_4_avx512bw_byteswap(fletcher_4_ctx_t * ctx,const void * buf,uint64_t size)171eda14cbcSMatt Macy fletcher_4_avx512bw_byteswap(fletcher_4_ctx_t *ctx, const void *buf,
172eda14cbcSMatt Macy uint64_t size)
173eda14cbcSMatt Macy {
174eda14cbcSMatt Macy static const zfs_fletcher_avx512_t mask = {
175eda14cbcSMatt Macy .v = { 0xFFFFFFFF00010203, 0xFFFFFFFF08090A0B,
176eda14cbcSMatt Macy 0xFFFFFFFF00010203, 0xFFFFFFFF08090A0B,
177eda14cbcSMatt Macy 0xFFFFFFFF00010203, 0xFFFFFFFF08090A0B,
178eda14cbcSMatt Macy 0xFFFFFFFF00010203, 0xFFFFFFFF08090A0B }
179eda14cbcSMatt Macy };
180eda14cbcSMatt Macy const uint32_t *ip = buf;
181eda14cbcSMatt Macy const uint32_t *ipend = (uint32_t *)((uint8_t *)ip + size);
182eda14cbcSMatt Macy
183eda14cbcSMatt Macy FLETCHER_4_AVX512_RESTORE_CTX(ctx);
184eda14cbcSMatt Macy
185eda14cbcSMatt Macy __asm("vmovdqu64 %0, %%zmm5" :: "m" (mask));
186eda14cbcSMatt Macy
187bb2d13b6SMartin Matuska do {
188eda14cbcSMatt Macy __asm("vpmovzxdq %0, %%zmm4"::"m" (*ip));
189eda14cbcSMatt Macy
190eda14cbcSMatt Macy __asm("vpshufb %zmm5, %zmm4, %zmm4");
191eda14cbcSMatt Macy
192eda14cbcSMatt Macy __asm("vpaddq %zmm4, %zmm0, %zmm0");
193eda14cbcSMatt Macy __asm("vpaddq %zmm0, %zmm1, %zmm1");
194eda14cbcSMatt Macy __asm("vpaddq %zmm1, %zmm2, %zmm2");
195eda14cbcSMatt Macy __asm("vpaddq %zmm2, %zmm3, %zmm3");
196bb2d13b6SMartin Matuska } while ((ip += 8) < ipend);
197eda14cbcSMatt Macy
198eda14cbcSMatt Macy FLETCHER_4_AVX512_SAVE_CTX(ctx)
199eda14cbcSMatt Macy }
200eda14cbcSMatt Macy STACK_FRAME_NON_STANDARD(fletcher_4_avx512bw_byteswap);
201eda14cbcSMatt Macy
20216038816SMartin Matuska static boolean_t
fletcher_4_avx512bw_valid(void)20316038816SMartin Matuska fletcher_4_avx512bw_valid(void)
20416038816SMartin Matuska {
20516038816SMartin Matuska return (fletcher_4_avx512f_valid() && zfs_avx512bw_available());
20616038816SMartin Matuska }
20716038816SMartin Matuska
208eda14cbcSMatt Macy const fletcher_4_ops_t fletcher_4_avx512bw_ops = {
209eda14cbcSMatt Macy .init_native = fletcher_4_avx512f_init,
210eda14cbcSMatt Macy .fini_native = fletcher_4_avx512f_fini,
211eda14cbcSMatt Macy .compute_native = fletcher_4_avx512f_native,
212eda14cbcSMatt Macy .init_byteswap = fletcher_4_avx512f_init,
213eda14cbcSMatt Macy .fini_byteswap = fletcher_4_avx512f_fini,
214eda14cbcSMatt Macy .compute_byteswap = fletcher_4_avx512bw_byteswap,
21516038816SMartin Matuska .valid = fletcher_4_avx512bw_valid,
216*2a58b312SMartin Matuska .uses_fpu = B_TRUE,
217eda14cbcSMatt Macy .name = "avx512bw"
218eda14cbcSMatt Macy };
219eda14cbcSMatt Macy #endif
220eda14cbcSMatt Macy
221eda14cbcSMatt Macy #endif /* defined(__x86_64) && defined(HAVE_AVX512F) */
222