xref: /freebsd/sys/contrib/openzfs/module/zcommon/zfs_fletcher_superscalar4.c (revision 61145dc2b94f12f6a47344fb9aac702321880e43)
1*61145dc2SMartin Matuska // SPDX-License-Identifier: BSD-2-Clause OR GPL-2.0-only
2eda14cbcSMatt Macy /*
3eda14cbcSMatt Macy  * Implement fast Fletcher4 using superscalar pipelines.
4eda14cbcSMatt Macy  *
5eda14cbcSMatt Macy  * Use regular C code to compute
6eda14cbcSMatt Macy  * Fletcher4 in four incremental 64-bit parallel accumulator streams,
7eda14cbcSMatt Macy  * and then combine the streams to form the final four checksum words.
8eda14cbcSMatt Macy  * This implementation is a derivative of the AVX SIMD implementation by
9eda14cbcSMatt Macy  * James Guilford and Jinshan Xiong from Intel (see zfs_fletcher_intel.c).
10eda14cbcSMatt Macy  *
11eda14cbcSMatt Macy  * Copyright (C) 2016 Romain Dolbeau.
12eda14cbcSMatt Macy  *
13eda14cbcSMatt Macy  * Authors:
14eda14cbcSMatt Macy  *	Romain Dolbeau <romain.dolbeau@atos.net>
15eda14cbcSMatt Macy  *
16eda14cbcSMatt Macy  * This software is available to you under a choice of one of two
17eda14cbcSMatt Macy  * licenses.  You may choose to be licensed under the terms of the GNU
18eda14cbcSMatt Macy  * General Public License (GPL) Version 2, available from the file
19eda14cbcSMatt Macy  * COPYING in the main directory of this source tree, or the
20eda14cbcSMatt Macy  * OpenIB.org BSD license below:
21eda14cbcSMatt Macy  *
22eda14cbcSMatt Macy  *     Redistribution and use in source and binary forms, with or
23eda14cbcSMatt Macy  *     without modification, are permitted provided that the following
24eda14cbcSMatt Macy  *     conditions are met:
25eda14cbcSMatt Macy  *
26eda14cbcSMatt Macy  *	- Redistributions of source code must retain the above
27eda14cbcSMatt Macy  *	  copyright notice, this list of conditions and the following
28eda14cbcSMatt Macy  *	  disclaimer.
29eda14cbcSMatt Macy  *
30eda14cbcSMatt Macy  *	- Redistributions in binary form must reproduce the above
31eda14cbcSMatt Macy  *	  copyright notice, this list of conditions and the following
32eda14cbcSMatt Macy  *	  disclaimer in the documentation and/or other materials
33eda14cbcSMatt Macy  *	  provided with the distribution.
34eda14cbcSMatt Macy  *
35eda14cbcSMatt Macy  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
36eda14cbcSMatt Macy  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
37eda14cbcSMatt Macy  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
38eda14cbcSMatt Macy  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
39eda14cbcSMatt Macy  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
40eda14cbcSMatt Macy  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
41eda14cbcSMatt Macy  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
42eda14cbcSMatt Macy  * SOFTWARE.
43eda14cbcSMatt Macy  */
44eda14cbcSMatt Macy 
45eda14cbcSMatt Macy #include <sys/param.h>
46eda14cbcSMatt Macy #include <sys/byteorder.h>
47eda14cbcSMatt Macy #include <sys/spa_checksum.h>
48da5137abSMartin Matuska #include <sys/string.h>
49eda14cbcSMatt Macy #include <zfs_fletcher.h>
50eda14cbcSMatt Macy 
51eda14cbcSMatt Macy static void
fletcher_4_superscalar4_init(fletcher_4_ctx_t * ctx)52eda14cbcSMatt Macy fletcher_4_superscalar4_init(fletcher_4_ctx_t *ctx)
53eda14cbcSMatt Macy {
54da5137abSMartin Matuska 	memset(ctx->superscalar, 0, 4 * sizeof (zfs_fletcher_superscalar_t));
55eda14cbcSMatt Macy }
56eda14cbcSMatt Macy 
57eda14cbcSMatt Macy static void
fletcher_4_superscalar4_fini(fletcher_4_ctx_t * ctx,zio_cksum_t * zcp)58eda14cbcSMatt Macy fletcher_4_superscalar4_fini(fletcher_4_ctx_t *ctx, zio_cksum_t *zcp)
59eda14cbcSMatt Macy {
60eda14cbcSMatt Macy 	uint64_t A, B, C, D;
61eda14cbcSMatt Macy 
62eda14cbcSMatt Macy 	A = ctx->superscalar[0].v[0] + ctx->superscalar[0].v[1] +
63eda14cbcSMatt Macy 	    ctx->superscalar[0].v[2] + ctx->superscalar[0].v[3];
64eda14cbcSMatt Macy 	B = 0 - ctx->superscalar[0].v[1] - 2 * ctx->superscalar[0].v[2] -
65eda14cbcSMatt Macy 	    3 * ctx->superscalar[0].v[3] + 4 * ctx->superscalar[1].v[0] +
66eda14cbcSMatt Macy 	    4 * ctx->superscalar[1].v[1] + 4 * ctx->superscalar[1].v[2] +
67eda14cbcSMatt Macy 	    4 * ctx->superscalar[1].v[3];
68eda14cbcSMatt Macy 
69eda14cbcSMatt Macy 	C = ctx->superscalar[0].v[2] + 3 * ctx->superscalar[0].v[3] -
70eda14cbcSMatt Macy 	    6 * ctx->superscalar[1].v[0] - 10 * ctx->superscalar[1].v[1] -
71eda14cbcSMatt Macy 	    14 * ctx->superscalar[1].v[2] - 18 * ctx->superscalar[1].v[3] +
72eda14cbcSMatt Macy 	    16 * ctx->superscalar[2].v[0] + 16 * ctx->superscalar[2].v[1] +
73eda14cbcSMatt Macy 	    16 * ctx->superscalar[2].v[2] + 16 * ctx->superscalar[2].v[3];
74eda14cbcSMatt Macy 
75eda14cbcSMatt Macy 	D = 0 - ctx->superscalar[0].v[3] + 4 * ctx->superscalar[1].v[0] +
76eda14cbcSMatt Macy 	    10 * ctx->superscalar[1].v[1] + 20 * ctx->superscalar[1].v[2] +
77eda14cbcSMatt Macy 	    34 * ctx->superscalar[1].v[3] - 48 * ctx->superscalar[2].v[0] -
78eda14cbcSMatt Macy 	    64 * ctx->superscalar[2].v[1] - 80 * ctx->superscalar[2].v[2] -
79eda14cbcSMatt Macy 	    96 * ctx->superscalar[2].v[3] + 64 * ctx->superscalar[3].v[0] +
80eda14cbcSMatt Macy 	    64 * ctx->superscalar[3].v[1] + 64 * ctx->superscalar[3].v[2] +
81eda14cbcSMatt Macy 	    64 * ctx->superscalar[3].v[3];
82eda14cbcSMatt Macy 
83eda14cbcSMatt Macy 	ZIO_SET_CHECKSUM(zcp, A, B, C, D);
84eda14cbcSMatt Macy }
85eda14cbcSMatt Macy 
86eda14cbcSMatt Macy static void
fletcher_4_superscalar4_native(fletcher_4_ctx_t * ctx,const void * buf,uint64_t size)87eda14cbcSMatt Macy fletcher_4_superscalar4_native(fletcher_4_ctx_t *ctx,
88eda14cbcSMatt Macy     const void *buf, uint64_t size)
89eda14cbcSMatt Macy {
90eda14cbcSMatt Macy 	const uint32_t *ip = buf;
91eda14cbcSMatt Macy 	const uint32_t *ipend = ip + (size / sizeof (uint32_t));
92eda14cbcSMatt Macy 	uint64_t a, b, c, d;
93eda14cbcSMatt Macy 	uint64_t a2, b2, c2, d2;
94eda14cbcSMatt Macy 	uint64_t a3, b3, c3, d3;
95eda14cbcSMatt Macy 	uint64_t a4, b4, c4, d4;
96eda14cbcSMatt Macy 
97eda14cbcSMatt Macy 	a = ctx->superscalar[0].v[0];
98eda14cbcSMatt Macy 	b = ctx->superscalar[1].v[0];
99eda14cbcSMatt Macy 	c = ctx->superscalar[2].v[0];
100eda14cbcSMatt Macy 	d = ctx->superscalar[3].v[0];
101eda14cbcSMatt Macy 	a2 = ctx->superscalar[0].v[1];
102eda14cbcSMatt Macy 	b2 = ctx->superscalar[1].v[1];
103eda14cbcSMatt Macy 	c2 = ctx->superscalar[2].v[1];
104eda14cbcSMatt Macy 	d2 = ctx->superscalar[3].v[1];
105eda14cbcSMatt Macy 	a3 = ctx->superscalar[0].v[2];
106eda14cbcSMatt Macy 	b3 = ctx->superscalar[1].v[2];
107eda14cbcSMatt Macy 	c3 = ctx->superscalar[2].v[2];
108eda14cbcSMatt Macy 	d3 = ctx->superscalar[3].v[2];
109eda14cbcSMatt Macy 	a4 = ctx->superscalar[0].v[3];
110eda14cbcSMatt Macy 	b4 = ctx->superscalar[1].v[3];
111eda14cbcSMatt Macy 	c4 = ctx->superscalar[2].v[3];
112eda14cbcSMatt Macy 	d4 = ctx->superscalar[3].v[3];
113eda14cbcSMatt Macy 
114bb2d13b6SMartin Matuska 	do {
115eda14cbcSMatt Macy 		a += ip[0];
116eda14cbcSMatt Macy 		a2 += ip[1];
117eda14cbcSMatt Macy 		a3 += ip[2];
118eda14cbcSMatt Macy 		a4 += ip[3];
119eda14cbcSMatt Macy 		b += a;
120eda14cbcSMatt Macy 		b2 += a2;
121eda14cbcSMatt Macy 		b3 += a3;
122eda14cbcSMatt Macy 		b4 += a4;
123eda14cbcSMatt Macy 		c += b;
124eda14cbcSMatt Macy 		c2 += b2;
125eda14cbcSMatt Macy 		c3 += b3;
126eda14cbcSMatt Macy 		c4 += b4;
127eda14cbcSMatt Macy 		d += c;
128eda14cbcSMatt Macy 		d2 += c2;
129eda14cbcSMatt Macy 		d3 += c3;
130eda14cbcSMatt Macy 		d4 += c4;
131bb2d13b6SMartin Matuska 	} while ((ip += 4) < ipend);
132eda14cbcSMatt Macy 
133eda14cbcSMatt Macy 	ctx->superscalar[0].v[0] = a;
134eda14cbcSMatt Macy 	ctx->superscalar[1].v[0] = b;
135eda14cbcSMatt Macy 	ctx->superscalar[2].v[0] = c;
136eda14cbcSMatt Macy 	ctx->superscalar[3].v[0] = d;
137eda14cbcSMatt Macy 	ctx->superscalar[0].v[1] = a2;
138eda14cbcSMatt Macy 	ctx->superscalar[1].v[1] = b2;
139eda14cbcSMatt Macy 	ctx->superscalar[2].v[1] = c2;
140eda14cbcSMatt Macy 	ctx->superscalar[3].v[1] = d2;
141eda14cbcSMatt Macy 	ctx->superscalar[0].v[2] = a3;
142eda14cbcSMatt Macy 	ctx->superscalar[1].v[2] = b3;
143eda14cbcSMatt Macy 	ctx->superscalar[2].v[2] = c3;
144eda14cbcSMatt Macy 	ctx->superscalar[3].v[2] = d3;
145eda14cbcSMatt Macy 	ctx->superscalar[0].v[3] = a4;
146eda14cbcSMatt Macy 	ctx->superscalar[1].v[3] = b4;
147eda14cbcSMatt Macy 	ctx->superscalar[2].v[3] = c4;
148eda14cbcSMatt Macy 	ctx->superscalar[3].v[3] = d4;
149eda14cbcSMatt Macy }
150eda14cbcSMatt Macy 
151eda14cbcSMatt Macy static void
fletcher_4_superscalar4_byteswap(fletcher_4_ctx_t * ctx,const void * buf,uint64_t size)152eda14cbcSMatt Macy fletcher_4_superscalar4_byteswap(fletcher_4_ctx_t *ctx,
153eda14cbcSMatt Macy     const void *buf, uint64_t size)
154eda14cbcSMatt Macy {
155eda14cbcSMatt Macy 	const uint32_t *ip = buf;
156eda14cbcSMatt Macy 	const uint32_t *ipend = ip + (size / sizeof (uint32_t));
157eda14cbcSMatt Macy 	uint64_t a, b, c, d;
158eda14cbcSMatt Macy 	uint64_t a2, b2, c2, d2;
159eda14cbcSMatt Macy 	uint64_t a3, b3, c3, d3;
160eda14cbcSMatt Macy 	uint64_t a4, b4, c4, d4;
161eda14cbcSMatt Macy 
162eda14cbcSMatt Macy 	a = ctx->superscalar[0].v[0];
163eda14cbcSMatt Macy 	b = ctx->superscalar[1].v[0];
164eda14cbcSMatt Macy 	c = ctx->superscalar[2].v[0];
165eda14cbcSMatt Macy 	d = ctx->superscalar[3].v[0];
166eda14cbcSMatt Macy 	a2 = ctx->superscalar[0].v[1];
167eda14cbcSMatt Macy 	b2 = ctx->superscalar[1].v[1];
168eda14cbcSMatt Macy 	c2 = ctx->superscalar[2].v[1];
169eda14cbcSMatt Macy 	d2 = ctx->superscalar[3].v[1];
170eda14cbcSMatt Macy 	a3 = ctx->superscalar[0].v[2];
171eda14cbcSMatt Macy 	b3 = ctx->superscalar[1].v[2];
172eda14cbcSMatt Macy 	c3 = ctx->superscalar[2].v[2];
173eda14cbcSMatt Macy 	d3 = ctx->superscalar[3].v[2];
174eda14cbcSMatt Macy 	a4 = ctx->superscalar[0].v[3];
175eda14cbcSMatt Macy 	b4 = ctx->superscalar[1].v[3];
176eda14cbcSMatt Macy 	c4 = ctx->superscalar[2].v[3];
177eda14cbcSMatt Macy 	d4 = ctx->superscalar[3].v[3];
178eda14cbcSMatt Macy 
179bb2d13b6SMartin Matuska 	do {
180eda14cbcSMatt Macy 		a += BSWAP_32(ip[0]);
181eda14cbcSMatt Macy 		a2 += BSWAP_32(ip[1]);
182eda14cbcSMatt Macy 		a3 += BSWAP_32(ip[2]);
183eda14cbcSMatt Macy 		a4 += BSWAP_32(ip[3]);
184eda14cbcSMatt Macy 		b += a;
185eda14cbcSMatt Macy 		b2 += a2;
186eda14cbcSMatt Macy 		b3 += a3;
187eda14cbcSMatt Macy 		b4 += a4;
188eda14cbcSMatt Macy 		c += b;
189eda14cbcSMatt Macy 		c2 += b2;
190eda14cbcSMatt Macy 		c3 += b3;
191eda14cbcSMatt Macy 		c4 += b4;
192eda14cbcSMatt Macy 		d += c;
193eda14cbcSMatt Macy 		d2 += c2;
194eda14cbcSMatt Macy 		d3 += c3;
195eda14cbcSMatt Macy 		d4 += c4;
196bb2d13b6SMartin Matuska 	} while ((ip += 4) < ipend);
197eda14cbcSMatt Macy 
198eda14cbcSMatt Macy 	ctx->superscalar[0].v[0] = a;
199eda14cbcSMatt Macy 	ctx->superscalar[1].v[0] = b;
200eda14cbcSMatt Macy 	ctx->superscalar[2].v[0] = c;
201eda14cbcSMatt Macy 	ctx->superscalar[3].v[0] = d;
202eda14cbcSMatt Macy 	ctx->superscalar[0].v[1] = a2;
203eda14cbcSMatt Macy 	ctx->superscalar[1].v[1] = b2;
204eda14cbcSMatt Macy 	ctx->superscalar[2].v[1] = c2;
205eda14cbcSMatt Macy 	ctx->superscalar[3].v[1] = d2;
206eda14cbcSMatt Macy 	ctx->superscalar[0].v[2] = a3;
207eda14cbcSMatt Macy 	ctx->superscalar[1].v[2] = b3;
208eda14cbcSMatt Macy 	ctx->superscalar[2].v[2] = c3;
209eda14cbcSMatt Macy 	ctx->superscalar[3].v[2] = d3;
210eda14cbcSMatt Macy 	ctx->superscalar[0].v[3] = a4;
211eda14cbcSMatt Macy 	ctx->superscalar[1].v[3] = b4;
212eda14cbcSMatt Macy 	ctx->superscalar[2].v[3] = c4;
213eda14cbcSMatt Macy 	ctx->superscalar[3].v[3] = d4;
214eda14cbcSMatt Macy }
215eda14cbcSMatt Macy 
fletcher_4_superscalar4_valid(void)216eda14cbcSMatt Macy static boolean_t fletcher_4_superscalar4_valid(void)
217eda14cbcSMatt Macy {
218eda14cbcSMatt Macy 	return (B_TRUE);
219eda14cbcSMatt Macy }
220eda14cbcSMatt Macy 
221eda14cbcSMatt Macy const fletcher_4_ops_t fletcher_4_superscalar4_ops = {
222eda14cbcSMatt Macy 	.init_native = fletcher_4_superscalar4_init,
223eda14cbcSMatt Macy 	.compute_native = fletcher_4_superscalar4_native,
224eda14cbcSMatt Macy 	.fini_native = fletcher_4_superscalar4_fini,
225eda14cbcSMatt Macy 	.init_byteswap = fletcher_4_superscalar4_init,
226eda14cbcSMatt Macy 	.compute_byteswap = fletcher_4_superscalar4_byteswap,
227eda14cbcSMatt Macy 	.fini_byteswap = fletcher_4_superscalar4_fini,
228eda14cbcSMatt Macy 	.valid = fletcher_4_superscalar4_valid,
2292a58b312SMartin Matuska 	.uses_fpu = B_FALSE,
230eda14cbcSMatt Macy 	.name = "superscalar4"
231eda14cbcSMatt Macy };
232