fletcher.c revision 256281
1276789Sdim/*
2276789Sdim * CDDL HEADER START
3353358Sdim *
4353358Sdim * The contents of this file are subject to the terms of the
5353358Sdim * Common Development and Distribution License (the "License").
6276789Sdim * You may not use this file except in compliance with the License.
7276789Sdim *
8276789Sdim * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9276789Sdim * or http://www.opensolaris.org/os/licensing.
10276789Sdim * See the License for the specific language governing permissions
11276789Sdim * and limitations under the License.
12327952Sdim *
13327952Sdim * When distributing Covered Code, include this CDDL HEADER in each
14276789Sdim * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15276789Sdim * If applicable, add the following below this CDDL HEADER, with the
16276789Sdim * fields enclosed by brackets "[]" replaced with your own identifying
17276789Sdim * information: Portions Copyright [yyyy] [name of copyright owner]
18276789Sdim *
19276789Sdim * CDDL HEADER END
20276789Sdim */
21276789Sdim/*
22276789Sdim * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
23276789Sdim * Use is subject to license terms.
24276789Sdim */
25276789Sdim
26276789Sdim/*#pragma ident	"%Z%%M%	%I%	%E% SMI"*/
27276789Sdim
28276789Sdimstatic void
29276789Sdimfletcher_2_native(const void *buf, uint64_t size, zio_cksum_t *zcp)
30276789Sdim{
31276789Sdim	const uint64_t *ip = buf;
32276789Sdim	const uint64_t *ipend = ip + (size / sizeof (uint64_t));
33276789Sdim	uint64_t a0, b0, a1, b1;
34276789Sdim
35276789Sdim	for (a0 = b0 = a1 = b1 = 0; ip < ipend; ip += 2) {
36276789Sdim		a0 += ip[0];
37276789Sdim		a1 += ip[1];
38276789Sdim		b0 += a0;
39276789Sdim		b1 += a1;
40276789Sdim	}
41296417Sdim
42296417Sdim	ZIO_SET_CHECKSUM(zcp, a0, a1, b0, b1);
43296417Sdim}
44360784Sdim
45360784Sdimstatic void
46360784Sdimfletcher_2_byteswap(const void *buf, uint64_t size, zio_cksum_t *zcp)
47360784Sdim{
48360784Sdim	const uint64_t *ip = buf;
49360784Sdim	const uint64_t *ipend = ip + (size / sizeof (uint64_t));
50296417Sdim	uint64_t a0, b0, a1, b1;
51296417Sdim
52296417Sdim	for (a0 = b0 = a1 = b1 = 0; ip < ipend; ip += 2) {
53296417Sdim		a0 += BSWAP_64(ip[0]);
54296417Sdim		a1 += BSWAP_64(ip[1]);
55360784Sdim		b0 += a0;
56296417Sdim		b1 += a1;
57296417Sdim	}
58321369Sdim
59321369Sdim	ZIO_SET_CHECKSUM(zcp, a0, a1, b0, b1);
60321369Sdim}
61296417Sdim
62321369Sdimstatic void
63321369Sdimfletcher_4_native(const void *buf, uint64_t size, zio_cksum_t *zcp)
64321369Sdim{
65321369Sdim	const uint32_t *ip = buf;
66321369Sdim	const uint32_t *ipend = ip + (size / sizeof (uint32_t));
67296417Sdim	uint64_t a, b, c, d;
68296417Sdim
69296417Sdim	for (a = b = c = d = 0; ip < ipend; ip++) {
70296417Sdim		a += ip[0];
71321369Sdim		b += a;
72321369Sdim		c += b;
73296417Sdim		d += c;
74296417Sdim	}
75296417Sdim
76296417Sdim	ZIO_SET_CHECKSUM(zcp, a, b, c, d);
77296417Sdim}
78296417Sdim
79296417Sdimstatic void
80296417Sdimfletcher_4_byteswap(const void *buf, uint64_t size, zio_cksum_t *zcp)
81296417Sdim{
82309124Sdim	const uint32_t *ip = buf;
83309124Sdim	const uint32_t *ipend = ip + (size / sizeof (uint32_t));
84296417Sdim	uint64_t a, b, c, d;
85296417Sdim
86296417Sdim	for (a = b = c = d = 0; ip < ipend; ip++) {
87321369Sdim		a += BSWAP_32(ip[0]);
88309124Sdim		b += a;
89296417Sdim		c += b;
90296417Sdim		d += c;
91296417Sdim	}
92296417Sdim
93309124Sdim	ZIO_SET_CHECKSUM(zcp, a, b, c, d);
94296417Sdim}
95296417Sdim