xref: /titanic_50/usr/src/common/zfs/zfs_fletcher.c (revision 45818ee124adeaaf947698996b4f4c722afc6d1f)
1495db6fbSLori Alt /*
2495db6fbSLori Alt  * CDDL HEADER START
3495db6fbSLori Alt  *
4495db6fbSLori Alt  * The contents of this file are subject to the terms of the
5495db6fbSLori Alt  * Common Development and Distribution License (the "License").
6495db6fbSLori Alt  * You may not use this file except in compliance with the License.
7495db6fbSLori Alt  *
8495db6fbSLori Alt  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9495db6fbSLori Alt  * or http://www.opensolaris.org/os/licensing.
10495db6fbSLori Alt  * See the License for the specific language governing permissions
11495db6fbSLori Alt  * and limitations under the License.
12495db6fbSLori Alt  *
13495db6fbSLori Alt  * When distributing Covered Code, include this CDDL HEADER in each
14495db6fbSLori Alt  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15495db6fbSLori Alt  * If applicable, add the following below this CDDL HEADER, with the
16495db6fbSLori Alt  * fields enclosed by brackets "[]" replaced with your own identifying
17495db6fbSLori Alt  * information: Portions Copyright [yyyy] [name of copyright owner]
18495db6fbSLori Alt  *
19495db6fbSLori Alt  * CDDL HEADER END
20495db6fbSLori Alt  */
21495db6fbSLori Alt /*
22495db6fbSLori Alt  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
23495db6fbSLori Alt  * Use is subject to license terms.
24495db6fbSLori Alt  */
25*45818ee1SMatthew Ahrens /*
26*45818ee1SMatthew Ahrens  * Copyright 2013 Saso Kiselkov. All rights reserved.
27*45818ee1SMatthew Ahrens  */
28495db6fbSLori Alt 
29495db6fbSLori Alt /*
30495db6fbSLori Alt  * Fletcher Checksums
31495db6fbSLori Alt  * ------------------
32495db6fbSLori Alt  *
33495db6fbSLori Alt  * ZFS's 2nd and 4th order Fletcher checksums are defined by the following
34495db6fbSLori Alt  * recurrence relations:
35495db6fbSLori Alt  *
36495db6fbSLori Alt  *	a  = a    + f
37495db6fbSLori Alt  *	 i    i-1    i-1
38495db6fbSLori Alt  *
39495db6fbSLori Alt  *	b  = b    + a
40495db6fbSLori Alt  *	 i    i-1    i
41495db6fbSLori Alt  *
42495db6fbSLori Alt  *	c  = c    + b		(fletcher-4 only)
43495db6fbSLori Alt  *	 i    i-1    i
44495db6fbSLori Alt  *
45495db6fbSLori Alt  *	d  = d    + c		(fletcher-4 only)
46495db6fbSLori Alt  *	 i    i-1    i
47495db6fbSLori Alt  *
48495db6fbSLori Alt  * Where
49495db6fbSLori Alt  *	a_0 = b_0 = c_0 = d_0 = 0
50495db6fbSLori Alt  * and
51495db6fbSLori Alt  *	f_0 .. f_(n-1) are the input data.
52495db6fbSLori Alt  *
53495db6fbSLori Alt  * Using standard techniques, these translate into the following series:
54495db6fbSLori Alt  *
55495db6fbSLori Alt  *	     __n_			     __n_
56495db6fbSLori Alt  *	     \   |			     \   |
57495db6fbSLori Alt  *	a  =  >     f			b  =  >     i * f
58495db6fbSLori Alt  *	 n   /___|   n - i		 n   /___|	 n - i
59495db6fbSLori Alt  *	     i = 1			     i = 1
60495db6fbSLori Alt  *
61495db6fbSLori Alt  *
62495db6fbSLori Alt  *	     __n_			     __n_
63495db6fbSLori Alt  *	     \   |  i*(i+1)		     \   |  i*(i+1)*(i+2)
64495db6fbSLori Alt  *	c  =  >     ------- f		d  =  >     ------------- f
65495db6fbSLori Alt  *	 n   /___|     2     n - i	 n   /___|	  6	   n - i
66495db6fbSLori Alt  *	     i = 1			     i = 1
67495db6fbSLori Alt  *
68495db6fbSLori Alt  * For fletcher-2, the f_is are 64-bit, and [ab]_i are 64-bit accumulators.
69495db6fbSLori Alt  * Since the additions are done mod (2^64), errors in the high bits may not
70495db6fbSLori Alt  * be noticed.  For this reason, fletcher-2 is deprecated.
71495db6fbSLori Alt  *
72495db6fbSLori Alt  * For fletcher-4, the f_is are 32-bit, and [abcd]_i are 64-bit accumulators.
73495db6fbSLori Alt  * A conservative estimate of how big the buffer can get before we overflow
74495db6fbSLori Alt  * can be estimated using f_i = 0xffffffff for all i:
75495db6fbSLori Alt  *
76495db6fbSLori Alt  * % bc
77495db6fbSLori Alt  *  f=2^32-1;d=0; for (i = 1; d<2^64; i++) { d += f*i*(i+1)*(i+2)/6 }; (i-1)*4
78495db6fbSLori Alt  * 2264
79495db6fbSLori Alt  *  quit
80495db6fbSLori Alt  * %
81495db6fbSLori Alt  *
82495db6fbSLori Alt  * So blocks of up to 2k will not overflow.  Our largest block size is
83495db6fbSLori Alt  * 128k, which has 32k 4-byte words, so we can compute the largest possible
84495db6fbSLori Alt  * accumulators, then divide by 2^64 to figure the max amount of overflow:
85495db6fbSLori Alt  *
86495db6fbSLori Alt  * % bc
87495db6fbSLori Alt  *  a=b=c=d=0; f=2^32-1; for (i=1; i<=32*1024; i++) { a+=f; b+=a; c+=b; d+=c }
88495db6fbSLori Alt  *  a/2^64;b/2^64;c/2^64;d/2^64
89495db6fbSLori Alt  * 0
90495db6fbSLori Alt  * 0
91495db6fbSLori Alt  * 1365
92495db6fbSLori Alt  * 11186858
93495db6fbSLori Alt  *  quit
94495db6fbSLori Alt  * %
95495db6fbSLori Alt  *
96495db6fbSLori Alt  * So a and b cannot overflow.  To make sure each bit of input has some
97495db6fbSLori Alt  * effect on the contents of c and d, we can look at what the factors of
98495db6fbSLori Alt  * the coefficients in the equations for c_n and d_n are.  The number of 2s
99495db6fbSLori Alt  * in the factors determines the lowest set bit in the multiplier.  Running
100495db6fbSLori Alt  * through the cases for n*(n+1)/2 reveals that the highest power of 2 is
101495db6fbSLori Alt  * 2^14, and for n*(n+1)*(n+2)/6 it is 2^15.  So while some data may overflow
102495db6fbSLori Alt  * the 64-bit accumulators, every bit of every f_i effects every accumulator,
103495db6fbSLori Alt  * even for 128k blocks.
104495db6fbSLori Alt  *
105495db6fbSLori Alt  * If we wanted to make a stronger version of fletcher4 (fletcher4c?),
106495db6fbSLori Alt  * we could do our calculations mod (2^32 - 1) by adding in the carries
107495db6fbSLori Alt  * periodically, and store the number of carries in the top 32-bits.
108495db6fbSLori Alt  *
109495db6fbSLori Alt  * --------------------
110495db6fbSLori Alt  * Checksum Performance
111495db6fbSLori Alt  * --------------------
112495db6fbSLori Alt  *
113495db6fbSLori Alt  * There are two interesting components to checksum performance: cached and
114495db6fbSLori Alt  * uncached performance.  With cached data, fletcher-2 is about four times
115495db6fbSLori Alt  * faster than fletcher-4.  With uncached data, the performance difference is
116495db6fbSLori Alt  * negligible, since the cost of a cache fill dominates the processing time.
117495db6fbSLori Alt  * Even though fletcher-4 is slower than fletcher-2, it is still a pretty
118495db6fbSLori Alt  * efficient pass over the data.
119495db6fbSLori Alt  *
120495db6fbSLori Alt  * In normal operation, the data which is being checksummed is in a buffer
121495db6fbSLori Alt  * which has been filled either by:
122495db6fbSLori Alt  *
123495db6fbSLori Alt  *	1. a compression step, which will be mostly cached, or
124495db6fbSLori Alt  *	2. a bcopy() or copyin(), which will be uncached (because the
125495db6fbSLori Alt  *	   copy is cache-bypassing).
126495db6fbSLori Alt  *
127495db6fbSLori Alt  * For both cached and uncached data, both fletcher checksums are much faster
128495db6fbSLori Alt  * than sha-256, and slower than 'off', which doesn't touch the data at all.
129495db6fbSLori Alt  */
130495db6fbSLori Alt 
131495db6fbSLori Alt #include <sys/types.h>
132495db6fbSLori Alt #include <sys/sysmacros.h>
133495db6fbSLori Alt #include <sys/byteorder.h>
134b24ab676SJeff Bonwick #include <sys/zio.h>
135495db6fbSLori Alt #include <sys/spa.h>
136495db6fbSLori Alt 
137*45818ee1SMatthew Ahrens /*ARGSUSED*/
138495db6fbSLori Alt void
fletcher_2_native(const void * buf,uint64_t size,const void * ctx_template,zio_cksum_t * zcp)139*45818ee1SMatthew Ahrens fletcher_2_native(const void *buf, uint64_t size,
140*45818ee1SMatthew Ahrens     const void *ctx_template, zio_cksum_t *zcp)
141495db6fbSLori Alt {
142495db6fbSLori Alt 	const uint64_t *ip = buf;
143495db6fbSLori Alt 	const uint64_t *ipend = ip + (size / sizeof (uint64_t));
144495db6fbSLori Alt 	uint64_t a0, b0, a1, b1;
145495db6fbSLori Alt 
146495db6fbSLori Alt 	for (a0 = b0 = a1 = b1 = 0; ip < ipend; ip += 2) {
147495db6fbSLori Alt 		a0 += ip[0];
148495db6fbSLori Alt 		a1 += ip[1];
149495db6fbSLori Alt 		b0 += a0;
150495db6fbSLori Alt 		b1 += a1;
151495db6fbSLori Alt 	}
152495db6fbSLori Alt 
153495db6fbSLori Alt 	ZIO_SET_CHECKSUM(zcp, a0, a1, b0, b1);
154495db6fbSLori Alt }
155495db6fbSLori Alt 
156*45818ee1SMatthew Ahrens /*ARGSUSED*/
157495db6fbSLori Alt void
fletcher_2_byteswap(const void * buf,uint64_t size,const void * ctx_template,zio_cksum_t * zcp)158*45818ee1SMatthew Ahrens fletcher_2_byteswap(const void *buf, uint64_t size,
159*45818ee1SMatthew Ahrens     const void *ctx_template, zio_cksum_t *zcp)
160495db6fbSLori Alt {
161495db6fbSLori Alt 	const uint64_t *ip = buf;
162495db6fbSLori Alt 	const uint64_t *ipend = ip + (size / sizeof (uint64_t));
163495db6fbSLori Alt 	uint64_t a0, b0, a1, b1;
164495db6fbSLori Alt 
165495db6fbSLori Alt 	for (a0 = b0 = a1 = b1 = 0; ip < ipend; ip += 2) {
166495db6fbSLori Alt 		a0 += BSWAP_64(ip[0]);
167495db6fbSLori Alt 		a1 += BSWAP_64(ip[1]);
168495db6fbSLori Alt 		b0 += a0;
169495db6fbSLori Alt 		b1 += a1;
170495db6fbSLori Alt 	}
171495db6fbSLori Alt 
172495db6fbSLori Alt 	ZIO_SET_CHECKSUM(zcp, a0, a1, b0, b1);
173495db6fbSLori Alt }
174495db6fbSLori Alt 
175*45818ee1SMatthew Ahrens /*ARGSUSED*/
176495db6fbSLori Alt void
fletcher_4_native(const void * buf,uint64_t size,const void * ctx_template,zio_cksum_t * zcp)177*45818ee1SMatthew Ahrens fletcher_4_native(const void *buf, uint64_t size,
178*45818ee1SMatthew Ahrens     const void *ctx_template, zio_cksum_t *zcp)
179495db6fbSLori Alt {
180495db6fbSLori Alt 	const uint32_t *ip = buf;
181495db6fbSLori Alt 	const uint32_t *ipend = ip + (size / sizeof (uint32_t));
182495db6fbSLori Alt 	uint64_t a, b, c, d;
183495db6fbSLori Alt 
184495db6fbSLori Alt 	for (a = b = c = d = 0; ip < ipend; ip++) {
185495db6fbSLori Alt 		a += ip[0];
186495db6fbSLori Alt 		b += a;
187495db6fbSLori Alt 		c += b;
188495db6fbSLori Alt 		d += c;
189495db6fbSLori Alt 	}
190495db6fbSLori Alt 
191495db6fbSLori Alt 	ZIO_SET_CHECKSUM(zcp, a, b, c, d);
192495db6fbSLori Alt }
193495db6fbSLori Alt 
194*45818ee1SMatthew Ahrens /*ARGSUSED*/
195495db6fbSLori Alt void
fletcher_4_byteswap(const void * buf,uint64_t size,const void * ctx_template,zio_cksum_t * zcp)196*45818ee1SMatthew Ahrens fletcher_4_byteswap(const void *buf, uint64_t size,
197*45818ee1SMatthew Ahrens     const void *ctx_template, zio_cksum_t *zcp)
198495db6fbSLori Alt {
199495db6fbSLori Alt 	const uint32_t *ip = buf;
200495db6fbSLori Alt 	const uint32_t *ipend = ip + (size / sizeof (uint32_t));
201495db6fbSLori Alt 	uint64_t a, b, c, d;
202495db6fbSLori Alt 
203495db6fbSLori Alt 	for (a = b = c = d = 0; ip < ipend; ip++) {
204495db6fbSLori Alt 		a += BSWAP_32(ip[0]);
205495db6fbSLori Alt 		b += a;
206495db6fbSLori Alt 		c += b;
207495db6fbSLori Alt 		d += c;
208495db6fbSLori Alt 	}
209495db6fbSLori Alt 
210495db6fbSLori Alt 	ZIO_SET_CHECKSUM(zcp, a, b, c, d);
211495db6fbSLori Alt }
212495db6fbSLori Alt 
213495db6fbSLori Alt void
fletcher_4_incremental_native(const void * buf,uint64_t size,zio_cksum_t * zcp)214495db6fbSLori Alt fletcher_4_incremental_native(const void *buf, uint64_t size,
215495db6fbSLori Alt     zio_cksum_t *zcp)
216495db6fbSLori Alt {
217495db6fbSLori Alt 	const uint32_t *ip = buf;
218495db6fbSLori Alt 	const uint32_t *ipend = ip + (size / sizeof (uint32_t));
219495db6fbSLori Alt 	uint64_t a, b, c, d;
220495db6fbSLori Alt 
221495db6fbSLori Alt 	a = zcp->zc_word[0];
222495db6fbSLori Alt 	b = zcp->zc_word[1];
223495db6fbSLori Alt 	c = zcp->zc_word[2];
224495db6fbSLori Alt 	d = zcp->zc_word[3];
225495db6fbSLori Alt 
226495db6fbSLori Alt 	for (; ip < ipend; ip++) {
227495db6fbSLori Alt 		a += ip[0];
228495db6fbSLori Alt 		b += a;
229495db6fbSLori Alt 		c += b;
230495db6fbSLori Alt 		d += c;
231495db6fbSLori Alt 	}
232495db6fbSLori Alt 
233495db6fbSLori Alt 	ZIO_SET_CHECKSUM(zcp, a, b, c, d);
234495db6fbSLori Alt }
235495db6fbSLori Alt 
236495db6fbSLori Alt void
fletcher_4_incremental_byteswap(const void * buf,uint64_t size,zio_cksum_t * zcp)237495db6fbSLori Alt fletcher_4_incremental_byteswap(const void *buf, uint64_t size,
238495db6fbSLori Alt     zio_cksum_t *zcp)
239495db6fbSLori Alt {
240495db6fbSLori Alt 	const uint32_t *ip = buf;
241495db6fbSLori Alt 	const uint32_t *ipend = ip + (size / sizeof (uint32_t));
242495db6fbSLori Alt 	uint64_t a, b, c, d;
243495db6fbSLori Alt 
244495db6fbSLori Alt 	a = zcp->zc_word[0];
245495db6fbSLori Alt 	b = zcp->zc_word[1];
246495db6fbSLori Alt 	c = zcp->zc_word[2];
247495db6fbSLori Alt 	d = zcp->zc_word[3];
248495db6fbSLori Alt 
249495db6fbSLori Alt 	for (; ip < ipend; ip++) {
250495db6fbSLori Alt 		a += BSWAP_32(ip[0]);
251495db6fbSLori Alt 		b += a;
252495db6fbSLori Alt 		c += b;
253495db6fbSLori Alt 		d += c;
254495db6fbSLori Alt 	}
255495db6fbSLori Alt 
256495db6fbSLori Alt 	ZIO_SET_CHECKSUM(zcp, a, b, c, d);
257495db6fbSLori Alt }
258