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