Patrick Welche <prlw1@cam.ac.uk>
[netbsd-mini2440.git] / external / cddl / osnet / dist / uts / common / fs / zfs / fletcher.c
blobedda3c9a9d3d90c4767c0c9a1be2721917686311
1 /*
2 * CDDL HEADER START
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
19 * CDDL HEADER END
22 * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
26 #pragma ident "%Z%%M% %I% %E% SMI"
28 #include <sys/types.h>
29 #include <sys/sysmacros.h>
30 #include <sys/byteorder.h>
31 #include <sys/spa.h>
33 void
34 fletcher_2_native(const void *buf, uint64_t size, zio_cksum_t *zcp)
36 const uint64_t *ip = buf;
37 const uint64_t *ipend = ip + (size / sizeof (uint64_t));
38 uint64_t a0, b0, a1, b1;
40 for (a0 = b0 = a1 = b1 = 0; ip < ipend; ip += 2) {
41 a0 += ip[0];
42 a1 += ip[1];
43 b0 += a0;
44 b1 += a1;
47 ZIO_SET_CHECKSUM(zcp, a0, a1, b0, b1);
50 void
51 fletcher_2_byteswap(const void *buf, uint64_t size, zio_cksum_t *zcp)
53 const uint64_t *ip = buf;
54 const uint64_t *ipend = ip + (size / sizeof (uint64_t));
55 uint64_t a0, b0, a1, b1;
57 for (a0 = b0 = a1 = b1 = 0; ip < ipend; ip += 2) {
58 a0 += BSWAP_64(ip[0]);
59 a1 += BSWAP_64(ip[1]);
60 b0 += a0;
61 b1 += a1;
64 ZIO_SET_CHECKSUM(zcp, a0, a1, b0, b1);
67 void
68 fletcher_4_native(const void *buf, uint64_t size, zio_cksum_t *zcp)
70 const uint32_t *ip = buf;
71 const uint32_t *ipend = ip + (size / sizeof (uint32_t));
72 uint64_t a, b, c, d;
74 for (a = b = c = d = 0; ip < ipend; ip++) {
75 a += ip[0];
76 b += a;
77 c += b;
78 d += c;
81 ZIO_SET_CHECKSUM(zcp, a, b, c, d);
84 void
85 fletcher_4_byteswap(const void *buf, uint64_t size, zio_cksum_t *zcp)
87 const uint32_t *ip = buf;
88 const uint32_t *ipend = ip + (size / sizeof (uint32_t));
89 uint64_t a, b, c, d;
91 for (a = b = c = d = 0; ip < ipend; ip++) {
92 a += BSWAP_32(ip[0]);
93 b += a;
94 c += b;
95 d += c;
98 ZIO_SET_CHECKSUM(zcp, a, b, c, d);
101 void
102 fletcher_4_incremental_native(const void *buf, uint64_t size,
103 zio_cksum_t *zcp)
105 const uint32_t *ip = buf;
106 const uint32_t *ipend = ip + (size / sizeof (uint32_t));
107 uint64_t a, b, c, d;
109 a = zcp->zc_word[0];
110 b = zcp->zc_word[1];
111 c = zcp->zc_word[2];
112 d = zcp->zc_word[3];
114 for (; ip < ipend; ip++) {
115 a += ip[0];
116 b += a;
117 c += b;
118 d += c;
121 ZIO_SET_CHECKSUM(zcp, a, b, c, d);
124 void
125 fletcher_4_incremental_byteswap(const void *buf, uint64_t size,
126 zio_cksum_t *zcp)
128 const uint32_t *ip = buf;
129 const uint32_t *ipend = ip + (size / sizeof (uint32_t));
130 uint64_t a, b, c, d;
132 a = zcp->zc_word[0];
133 b = zcp->zc_word[1];
134 c = zcp->zc_word[2];
135 d = zcp->zc_word[3];
137 for (; ip < ipend; ip++) {
138 a += BSWAP_32(ip[0]);
139 b += a;
140 c += b;
141 d += c;
144 ZIO_SET_CHECKSUM(zcp, a, b, c, d);