1eda14cbcSMatt Macy /*
2eda14cbcSMatt Macy * Implement fast Fletcher4 using superscalar pipelines.
3eda14cbcSMatt Macy *
4eda14cbcSMatt Macy * Use regular C code to compute
5eda14cbcSMatt Macy * Fletcher4 in two 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_superscalar_init(fletcher_4_ctx_t * ctx)51eda14cbcSMatt Macy fletcher_4_superscalar_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_superscalar_fini(fletcher_4_ctx_t * ctx,zio_cksum_t * zcp)57eda14cbcSMatt Macy fletcher_4_superscalar_fini(fletcher_4_ctx_t *ctx, zio_cksum_t *zcp)
58eda14cbcSMatt Macy {
59eda14cbcSMatt Macy uint64_t A, B, C, D;
60eda14cbcSMatt Macy A = ctx->superscalar[0].v[0] + ctx->superscalar[0].v[1];
61eda14cbcSMatt Macy B = 2 * ctx->superscalar[1].v[0] + 2 * ctx->superscalar[1].v[1] -
62eda14cbcSMatt Macy ctx->superscalar[0].v[1];
63eda14cbcSMatt Macy C = 4 * ctx->superscalar[2].v[0] - ctx->superscalar[1].v[0] +
64eda14cbcSMatt Macy 4 * ctx->superscalar[2].v[1] - 3 * ctx->superscalar[1].v[1];
65eda14cbcSMatt Macy D = 8 * ctx->superscalar[3].v[0] - 4 * ctx->superscalar[2].v[0] +
66eda14cbcSMatt Macy 8 * ctx->superscalar[3].v[1] - 8 * ctx->superscalar[2].v[1] +
67eda14cbcSMatt Macy ctx->superscalar[1].v[1];
68eda14cbcSMatt Macy ZIO_SET_CHECKSUM(zcp, A, B, C, D);
69eda14cbcSMatt Macy }
70eda14cbcSMatt Macy
71eda14cbcSMatt Macy static void
fletcher_4_superscalar_native(fletcher_4_ctx_t * ctx,const void * buf,uint64_t size)72eda14cbcSMatt Macy fletcher_4_superscalar_native(fletcher_4_ctx_t *ctx,
73eda14cbcSMatt Macy const void *buf, uint64_t size)
74eda14cbcSMatt Macy {
75eda14cbcSMatt Macy const uint32_t *ip = buf;
76eda14cbcSMatt Macy const uint32_t *ipend = ip + (size / sizeof (uint32_t));
77eda14cbcSMatt Macy uint64_t a, b, c, d;
78eda14cbcSMatt Macy uint64_t a2, b2, c2, d2;
79eda14cbcSMatt Macy
80eda14cbcSMatt Macy a = ctx->superscalar[0].v[0];
81eda14cbcSMatt Macy b = ctx->superscalar[1].v[0];
82eda14cbcSMatt Macy c = ctx->superscalar[2].v[0];
83eda14cbcSMatt Macy d = ctx->superscalar[3].v[0];
84eda14cbcSMatt Macy a2 = ctx->superscalar[0].v[1];
85eda14cbcSMatt Macy b2 = ctx->superscalar[1].v[1];
86eda14cbcSMatt Macy c2 = ctx->superscalar[2].v[1];
87eda14cbcSMatt Macy d2 = ctx->superscalar[3].v[1];
88eda14cbcSMatt Macy
89bb2d13b6SMartin Matuska do {
90eda14cbcSMatt Macy a += ip[0];
91eda14cbcSMatt Macy a2 += ip[1];
92eda14cbcSMatt Macy b += a;
93eda14cbcSMatt Macy b2 += a2;
94eda14cbcSMatt Macy c += b;
95eda14cbcSMatt Macy c2 += b2;
96eda14cbcSMatt Macy d += c;
97eda14cbcSMatt Macy d2 += c2;
98bb2d13b6SMartin Matuska } while ((ip += 2) < ipend);
99eda14cbcSMatt Macy
100eda14cbcSMatt Macy ctx->superscalar[0].v[0] = a;
101eda14cbcSMatt Macy ctx->superscalar[1].v[0] = b;
102eda14cbcSMatt Macy ctx->superscalar[2].v[0] = c;
103eda14cbcSMatt Macy ctx->superscalar[3].v[0] = d;
104eda14cbcSMatt Macy ctx->superscalar[0].v[1] = a2;
105eda14cbcSMatt Macy ctx->superscalar[1].v[1] = b2;
106eda14cbcSMatt Macy ctx->superscalar[2].v[1] = c2;
107eda14cbcSMatt Macy ctx->superscalar[3].v[1] = d2;
108eda14cbcSMatt Macy }
109eda14cbcSMatt Macy
110eda14cbcSMatt Macy static void
fletcher_4_superscalar_byteswap(fletcher_4_ctx_t * ctx,const void * buf,uint64_t size)111eda14cbcSMatt Macy fletcher_4_superscalar_byteswap(fletcher_4_ctx_t *ctx,
112eda14cbcSMatt Macy const void *buf, uint64_t size)
113eda14cbcSMatt Macy {
114eda14cbcSMatt Macy const uint32_t *ip = buf;
115eda14cbcSMatt Macy const uint32_t *ipend = ip + (size / sizeof (uint32_t));
116eda14cbcSMatt Macy uint64_t a, b, c, d;
117eda14cbcSMatt Macy uint64_t a2, b2, c2, d2;
118eda14cbcSMatt Macy
119eda14cbcSMatt Macy a = ctx->superscalar[0].v[0];
120eda14cbcSMatt Macy b = ctx->superscalar[1].v[0];
121eda14cbcSMatt Macy c = ctx->superscalar[2].v[0];
122eda14cbcSMatt Macy d = ctx->superscalar[3].v[0];
123eda14cbcSMatt Macy a2 = ctx->superscalar[0].v[1];
124eda14cbcSMatt Macy b2 = ctx->superscalar[1].v[1];
125eda14cbcSMatt Macy c2 = ctx->superscalar[2].v[1];
126eda14cbcSMatt Macy d2 = ctx->superscalar[3].v[1];
127eda14cbcSMatt Macy
128bb2d13b6SMartin Matuska do {
129eda14cbcSMatt Macy a += BSWAP_32(ip[0]);
130eda14cbcSMatt Macy a2 += BSWAP_32(ip[1]);
131eda14cbcSMatt Macy b += a;
132eda14cbcSMatt Macy b2 += a2;
133eda14cbcSMatt Macy c += b;
134eda14cbcSMatt Macy c2 += b2;
135eda14cbcSMatt Macy d += c;
136eda14cbcSMatt Macy d2 += c2;
137bb2d13b6SMartin Matuska } while ((ip += 2) < ipend);
138eda14cbcSMatt Macy
139eda14cbcSMatt Macy ctx->superscalar[0].v[0] = a;
140eda14cbcSMatt Macy ctx->superscalar[1].v[0] = b;
141eda14cbcSMatt Macy ctx->superscalar[2].v[0] = c;
142eda14cbcSMatt Macy ctx->superscalar[3].v[0] = d;
143eda14cbcSMatt Macy ctx->superscalar[0].v[1] = a2;
144eda14cbcSMatt Macy ctx->superscalar[1].v[1] = b2;
145eda14cbcSMatt Macy ctx->superscalar[2].v[1] = c2;
146eda14cbcSMatt Macy ctx->superscalar[3].v[1] = d2;
147eda14cbcSMatt Macy }
148eda14cbcSMatt Macy
fletcher_4_superscalar_valid(void)149eda14cbcSMatt Macy static boolean_t fletcher_4_superscalar_valid(void)
150eda14cbcSMatt Macy {
151eda14cbcSMatt Macy return (B_TRUE);
152eda14cbcSMatt Macy }
153eda14cbcSMatt Macy
154eda14cbcSMatt Macy const fletcher_4_ops_t fletcher_4_superscalar_ops = {
155eda14cbcSMatt Macy .init_native = fletcher_4_superscalar_init,
156eda14cbcSMatt Macy .compute_native = fletcher_4_superscalar_native,
157eda14cbcSMatt Macy .fini_native = fletcher_4_superscalar_fini,
158eda14cbcSMatt Macy .init_byteswap = fletcher_4_superscalar_init,
159eda14cbcSMatt Macy .compute_byteswap = fletcher_4_superscalar_byteswap,
160eda14cbcSMatt Macy .fini_byteswap = fletcher_4_superscalar_fini,
161eda14cbcSMatt Macy .valid = fletcher_4_superscalar_valid,
162*2a58b312SMartin Matuska .uses_fpu = B_FALSE,
163eda14cbcSMatt Macy .name = "superscalar"
164eda14cbcSMatt Macy };
165