1 | /* |
---|
2 | * Copyright (c) 1990 The Regents of the University of California. |
---|
3 | * 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 | * from tahoe: in_cksum.c 1.2 86/01/05 |
---|
34 | * from: @(#)in_cksum.c 1.3 (Berkeley) 1/19/91 |
---|
35 | * from: Id: in_cksum.c,v 1.8 1995/12/03 18:35:19 bde Exp |
---|
36 | * $Id$ |
---|
37 | */ |
---|
38 | |
---|
39 | #ifndef _MACHINE_IN_CKSUM_H_ |
---|
40 | #define _MACHINE_IN_CKSUM_H_ 1 |
---|
41 | |
---|
42 | #include <sys/cdefs.h> |
---|
43 | |
---|
44 | /* |
---|
45 | * It it useful to have an Internet checksum routine which is inlineable |
---|
46 | * and optimized specifically for the task of computing IP header checksums |
---|
47 | * in the normal case (where there are no options and the header length is |
---|
48 | * therefore always exactly five 32-bit words. |
---|
49 | */ |
---|
50 | |
---|
51 | /* |
---|
52 | * Optimized version for the i386 family |
---|
53 | */ |
---|
54 | |
---|
55 | #if (defined(__GNUC__) && defined(__i386__)) |
---|
56 | |
---|
57 | static __inline u_int |
---|
58 | in_cksum_hdr(const struct ip *ip) |
---|
59 | { |
---|
60 | register u_int sum = 0; |
---|
61 | |
---|
62 | #define ADD(n) \ |
---|
63 | __asm__ volatile ("addl " #n "(%2), %0" : "=r" (sum) : "0" (sum), "r" (ip)) |
---|
64 | #define ADDC(n) \ |
---|
65 | __asm__ volatile ("adcl " #n "(%2), %0" : "=r" (sum) : "0" (sum), "r" (ip)) |
---|
66 | #define MOP \ |
---|
67 | __asm__ volatile ("adcl $0, %0" : "=r" (sum) : "0" (sum)) |
---|
68 | |
---|
69 | ADD(0); |
---|
70 | ADDC(4); |
---|
71 | ADDC(8); |
---|
72 | ADDC(12); |
---|
73 | ADDC(16); |
---|
74 | MOP; |
---|
75 | sum = (sum & 0xffff) + (sum >> 16); |
---|
76 | if (sum > 0xffff) |
---|
77 | sum -= 0xffff; |
---|
78 | |
---|
79 | return ~sum & 0xffff; |
---|
80 | } |
---|
81 | |
---|
82 | static __inline void |
---|
83 | in_cksum_update(struct ip *ip) |
---|
84 | { |
---|
85 | int __tmpsum; |
---|
86 | __tmpsum = (int)ntohs(ip->ip_sum) + 256; |
---|
87 | ip->ip_sum = htons(__tmpsum + (__tmpsum >> 16)); |
---|
88 | } |
---|
89 | |
---|
90 | /* |
---|
91 | * Optimized version for the MC68xxx and Coldfire families |
---|
92 | */ |
---|
93 | |
---|
94 | #elif (defined(__GNUC__) && (defined(__mc68000__) || defined(__m68k__))) |
---|
95 | |
---|
96 | static __inline__ u_int |
---|
97 | in_cksum_hdr(const struct ip *ip) |
---|
98 | { |
---|
99 | register u_int *ap = (u_int *)ip; |
---|
100 | register u_int sum = *ap++; |
---|
101 | register u_int tmp; |
---|
102 | |
---|
103 | __asm__("addl %2@+,%0\n\t" |
---|
104 | "movel %2@+,%1\n\t" |
---|
105 | "addxl %1,%0\n\t" |
---|
106 | "movel %2@+,%1\n\t" |
---|
107 | "addxl %1,%0\n\t" |
---|
108 | "movel %2@,%1\n\t" |
---|
109 | "addxl %1,%0\n\t" |
---|
110 | "moveq #0,%1\n\t" |
---|
111 | "addxl %1,%0\n" : |
---|
112 | "=d" (sum), "=d" (tmp), "=a" (ap) : |
---|
113 | "0" (sum), "2" (ap)); |
---|
114 | sum = (sum & 0xffff) + (sum >> 16); |
---|
115 | if (sum > 0xffff) |
---|
116 | sum -= 0xffff; |
---|
117 | return ~sum & 0xffff; |
---|
118 | } |
---|
119 | |
---|
120 | /* |
---|
121 | * Optimized version for the PowerPC family |
---|
122 | */ |
---|
123 | |
---|
124 | #elif (defined(__GNUC__) && (defined(__PPC__) || defined(__ppc__))) |
---|
125 | |
---|
126 | static __inline u_int |
---|
127 | in_cksum_hdr(const struct ip *ip) |
---|
128 | { |
---|
129 | register u_int sum = 0; |
---|
130 | register u_int tmp; |
---|
131 | |
---|
132 | #define ADD(n) \ |
---|
133 | __asm__ volatile ("addc %0,%0,%2" : "=r" (sum) : "0" (sum), "r" (n)) |
---|
134 | #define ADDC(n) \ |
---|
135 | __asm__ volatile ("adde %0,%0,%2" : "=r" (sum) : "0" (sum), "r" (n)) |
---|
136 | #define MOP \ |
---|
137 | __asm__ volatile ("addic %0,%0,0" : "=r" (sum) : "0" (sum)) |
---|
138 | |
---|
139 | tmp = *(((u_int *) ip)); ADD(tmp); |
---|
140 | tmp = *(((u_int *) ip) + 1); ADDC(tmp); |
---|
141 | tmp = *(((u_int *) ip) + 2); ADDC(tmp); |
---|
142 | tmp = *(((u_int *) ip) + 3); ADDC(tmp); |
---|
143 | tmp = *(((u_int *) ip) + 4); ADDC(tmp); |
---|
144 | tmp = 0; ADDC(tmp); |
---|
145 | sum = (sum & 0xffff) + (sum >> 16); |
---|
146 | if (sum > 0xffff) |
---|
147 | sum -= 0xffff; |
---|
148 | |
---|
149 | return ~sum & 0xffff; |
---|
150 | } |
---|
151 | |
---|
152 | static __inline void |
---|
153 | in_cksum_update(struct ip *ip) |
---|
154 | { |
---|
155 | int __tmpsum; |
---|
156 | __tmpsum = (int)ntohs(ip->ip_sum) + 256; |
---|
157 | ip->ip_sum = htons(__tmpsum + (__tmpsum >> 16)); |
---|
158 | } |
---|
159 | |
---|
160 | /* |
---|
161 | * SPARC Version |
---|
162 | */ |
---|
163 | |
---|
164 | #elif (defined(__GNUC__) && defined(sparc)) |
---|
165 | |
---|
166 | static __inline u_int |
---|
167 | in_cksum_hdr(const struct ip *ip) |
---|
168 | { |
---|
169 | register u_int sum = 0; |
---|
170 | register u_int tmp_o2; |
---|
171 | register u_int tmp_o3; |
---|
172 | |
---|
173 | __asm__ volatile (" |
---|
174 | ld [%0], %1 ; \ |
---|
175 | ld [%0+4], %2 ; \ |
---|
176 | addcc %1, %2, %1 ; \ |
---|
177 | ld [%0+8], %2 ; \ |
---|
178 | addxcc %1, %2, %1 ; \ |
---|
179 | ld [%0+12], %2 ; \ |
---|
180 | addxcc %1, %2, %1 ; \ |
---|
181 | ld [%0+16], %2 ; \ |
---|
182 | addxcc %1, %2, %1 ; \ |
---|
183 | set 0x0ffff, %3 ; \ |
---|
184 | srl %1, 16, %2 ; \ |
---|
185 | and %1, %3, %1 ; \ |
---|
186 | addx %1, %2, %1 ; \ |
---|
187 | srl %1, 16, %1 ; \ |
---|
188 | add %1, %%g0, %1 ; \ |
---|
189 | neg %1 ; \ |
---|
190 | and %1, %3, %1 ; \ |
---|
191 | " : "=r" (ip), "=r" (sum), "=r" (tmp_o2), "=r" (tmp_o3) |
---|
192 | : "0" (ip), "1" (sum) |
---|
193 | ); |
---|
194 | return sum; |
---|
195 | } |
---|
196 | |
---|
197 | #define in_cksum_update(ip) \ |
---|
198 | do { \ |
---|
199 | int __tmpsum; \ |
---|
200 | __tmpsum = (int)ntohs(ip->ip_sum) + 256; \ |
---|
201 | ip->ip_sum = htons(__tmpsum + (__tmpsum >> 16)); \ |
---|
202 | } while(0) |
---|
203 | /* |
---|
204 | * Here is the generic, portable, inefficient algorithm. |
---|
205 | */ |
---|
206 | |
---|
207 | #else |
---|
208 | u_int in_cksum_hdr __P((const struct ip *)); |
---|
209 | #define in_cksum_update(ip) \ |
---|
210 | do { \ |
---|
211 | int __tmpsum; \ |
---|
212 | __tmpsum = (int)ntohs(ip->ip_sum) + 256; \ |
---|
213 | ip->ip_sum = htons(__tmpsum + (__tmpsum >> 16)); \ |
---|
214 | } while(0) |
---|
215 | |
---|
216 | #endif |
---|
217 | |
---|
218 | #endif /* _MACHINE_IN_CKSUM_H_ */ |
---|