Move around some #ifdefs to silence warnings.
[dragonfly.git] / sys / netproto / ns / ns_cksum.c
1 /*
2  * Copyright (c) 1982, 1992, 1993
3  *      The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *      This product includes software developed by the University of
16  *      California, Berkeley and its contributors.
17  * 4. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  *
33  *      @(#)ns_cksum.c  8.1 (Berkeley) 6/10/93
34  * $FreeBSD: src/sys/netns/ns_cksum.c,v 1.7 1999/08/28 00:49:49 peter Exp $
35  * $DragonFly: src/sys/netproto/ns/ns_cksum.c,v 1.3 2003/07/26 21:10:52 rob Exp $
36  */
37
38 #include <sys/param.h>
39 #include <sys/mbuf.h>
40
41 /*
42  * Checksum routine for Network Systems Protocol Packets (Big-Endian).
43  *
44  * This routine is very heavily used in the network
45  * code and should be modified for each CPU to be as fast as possible.
46  */
47
48 #define ADDCARRY(x)  { if ((x) > 65535) (x) -= 65535; }
49 #define FOLD(x) {l_util.l = (x); (x) = l_util.s[0] + l_util.s[1]; ADDCARRY(x);}
50
51 u_short
52 ns_cksum(m, len)
53         struct mbuf *m;
54         int len;
55 {
56         u_short *w;
57         int sum = 0;
58         int mlen = 0;
59         int sum2;
60
61         union {
62                 u_short s[2];
63                 long    l;
64         } l_util;
65
66         for (;m && len; m = m->m_next) {
67                 if (m->m_len == 0)
68                         continue;
69                 /*
70                  * Each trip around loop adds in
71                  * word from one mbuf segment.
72                  */
73                 w = mtod(m, u_short *);
74                 if (mlen == -1) {
75                         /*
76                          * There is a byte left from the last segment;
77                          * ones-complement add it into the checksum.
78                          */
79 #if BYTE_ORDER == BIG_ENDIAN
80                         sum  += *(u_char *)w;
81 #else
82                         sum  += *(u_char *)w << 8;
83 #endif
84                         sum += sum;
85                         w = (u_short *)(1 + (char *)w);
86                         mlen = m->m_len - 1;
87                         len--;
88                         FOLD(sum);
89                 } else
90                         mlen = m->m_len;
91                 if (len < mlen)
92                         mlen = len;
93                 len -= mlen;
94                 /*
95                  * We can do a 16 bit ones complement sum using
96                  * 32 bit arithmetic registers for adding,
97                  * with carries from the low added
98                  * into the high (by normal carry-chaining)
99                  * so long as we fold back before 16 carries have occured.
100                  */
101                 if (1 & (int) w)
102                         goto uuuuglyy;
103 #ifndef TINY
104 /* -DTINY reduces the size from 1250 to 550, but slows it down by 22% */
105                 while ((mlen -= 32) >= 0) {
106                         sum += w[0]; sum += sum; sum += w[1]; sum += sum;
107                         sum += w[2]; sum += sum; sum += w[3]; sum += sum;
108                         sum += w[4]; sum += sum; sum += w[5]; sum += sum;
109                         sum += w[6]; sum += sum; sum += w[7]; sum += sum;
110                         FOLD(sum);
111                         sum += w[8]; sum += sum; sum += w[9]; sum += sum;
112                         sum += w[10]; sum += sum; sum += w[11]; sum += sum;
113                         sum += w[12]; sum += sum; sum += w[13]; sum += sum;
114                         sum += w[14]; sum += sum; sum += w[15]; sum += sum;
115                         FOLD(sum);
116                         w += 16;
117                 }
118                 mlen += 32;
119 #endif
120                 while ((mlen -= 8) >= 0) {
121                         sum += w[0]; sum += sum; sum += w[1]; sum += sum;
122                         sum += w[2]; sum += sum; sum += w[3]; sum += sum;
123                         FOLD(sum);
124                         w += 4;
125                 }
126                 mlen += 8;
127                 while ((mlen -= 2) >= 0) {
128                         sum += *w++; sum += sum;
129                 }
130                 goto commoncase;
131 uuuuglyy:
132 #if BYTE_ORDER == BIG_ENDIAN
133 #define ww(n) (((u_char *)w)[n + n + 1])
134 #define vv(n) (((u_char *)w)[n + n])
135 #else
136 #if BYTE_ORDER == LITTLE_ENDIAN
137 #define vv(n) (((u_char *)w)[n + n + 1])
138 #define ww(n) (((u_char *)w)[n + n])
139 #endif
140 #endif
141                 sum2 = 0;
142 #ifndef TINY
143                 while ((mlen -= 32) >= 0) {
144                     sum += ww(0); sum += sum; sum += ww(1); sum += sum;
145                     sum += ww(2); sum += sum; sum += ww(3); sum += sum;
146                     sum += ww(4); sum += sum; sum += ww(5); sum += sum;
147                     sum += ww(6); sum += sum; sum += ww(7); sum += sum;
148                     FOLD(sum);
149                     sum += ww(8); sum += sum; sum += ww(9); sum += sum;
150                     sum += ww(10); sum += sum; sum += ww(11); sum += sum;
151                     sum += ww(12); sum += sum; sum += ww(13); sum += sum;
152                     sum += ww(14); sum += sum; sum += ww(15); sum += sum;
153                     FOLD(sum);
154                     sum2 += vv(0); sum2 += sum2; sum2 += vv(1); sum2 += sum2;
155                     sum2 += vv(2); sum2 += sum2; sum2 += vv(3); sum2 += sum2;
156                     sum2 += vv(4); sum2 += sum2; sum2 += vv(5); sum2 += sum2;
157                     sum2 += vv(6); sum2 += sum2; sum2 += vv(7); sum2 += sum2;
158                     FOLD(sum2);
159                     sum2 += vv(8); sum2 += sum2; sum2 += vv(9); sum2 += sum2;
160                     sum2 += vv(10); sum2 += sum2; sum2 += vv(11); sum2 += sum2;
161                     sum2 += vv(12); sum2 += sum2; sum2 += vv(13); sum2 += sum2;
162                     sum2 += vv(14); sum2 += sum2; sum2 += vv(15); sum2 += sum2;
163                     FOLD(sum2);
164                     w += 16;
165                 }
166                 mlen += 32;
167 #endif
168                 while ((mlen -= 8) >= 0) {
169                     sum += ww(0); sum += sum; sum += ww(1); sum += sum;
170                     sum += ww(2); sum += sum; sum += ww(3); sum += sum;
171                     FOLD(sum);
172                     sum2 += vv(0); sum2 += sum2; sum2 += vv(1); sum2 += sum2;
173                     sum2 += vv(2); sum2 += sum2; sum2 += vv(3); sum2 += sum2;
174                     FOLD(sum2);
175                     w += 4;
176                 }
177                 mlen += 8;
178                 while ((mlen -= 2) >= 0) {
179                         sum += ww(0); sum += sum;
180                         sum2 += vv(0); sum2 += sum2;
181                         w++;
182                 }
183                 sum += (sum2 << 8);
184 commoncase:
185                 if (mlen == -1) {
186 #if BYTE_ORDER == BIG_ENDIAN
187                         sum += *(u_char *)w << 8;
188 #else
189                         sum += *(u_char *)w;
190 #endif
191                 }
192                 FOLD(sum);
193         }
194         if (mlen == -1) {
195                 /* We had an odd number of bytes to sum; assume a garbage
196                    byte of zero and clean up */
197                 sum += sum;
198                 FOLD(sum);
199         }
200         /*
201          * sum has already been kept to low sixteen bits.
202          * just examine result and exit.
203          */
204         if(sum==0xffff) sum = 0;
205         return (sum);
206 }