source: rtems/cpukit/libnetworking/machine/in_cksum.h @ ee613aa

4.115
Last change on this file since ee613aa was ee613aa, checked in by Ralf Corsepius <ralf.corsepius@…>, on 03/24/11 at 09:36:14

Include <sys/cdefs.h> (Eliminate rtems/bsd/sys/cdefs.h).

  • Property mode set to 100644
File size: 6.4 KB
Line 
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#include <netinet/ip.h> /* struct ip */
44
45/*
46 * It it useful to have an Internet checksum routine which is inlineable
47 * and optimized specifically for the task of computing IP header checksums
48 * in the normal case (where there are no options and the header length is
49 * therefore always exactly five 32-bit words.
50 */
51
52/*
53 *  Optimized version for the i386 family
54 */
55
56#if (defined(__GNUC__) && defined(__i386__))
57
58static __inline u_int
59in_cksum_hdr(const struct ip *ip)
60{
61        register u_int sum = ((const uint32_t*)ip)[0];
62        register u_int tmp;
63
64        __asm__ __volatile__(
65                                "       addl %2, %0    \n"
66                                "       adcl %3, %0    \n"
67                                "       adcl %4, %0    \n"
68                                "       adcl %5, %0    \n"
69                                "       adcl $0, %0    \n"
70                                "       movl %0, %1    \n"
71                                "       roll $16, %0   \n"
72                                "       addl %1, %0    \n"
73                                :"+&r"(sum),"=&r"(tmp)
74                                :"g"(((const uint32_t*)ip)[1]),
75                                 "g"(((const uint32_t*)ip)[2]),
76                                 "g"(((const uint32_t*)ip)[3]),
77                                 "g"(((const uint32_t*)ip)[4]),
78                                 "m"(*ip)
79                                :"cc"
80        );
81                   
82        return (~sum) >>16;
83}
84
85static __inline void
86in_cksum_update(struct ip *ip)
87{
88        int __tmpsum;
89        __tmpsum = (int)ntohs(ip->ip_sum) + 256;
90        ip->ip_sum = htons(__tmpsum + (__tmpsum >> 16));
91}
92
93/*
94 *  Optimized version for the MC68xxx and Coldfire families
95 */
96
97#elif (defined(__GNUC__) && (defined(__mc68000__) || defined(__m68k__)))
98
99static __inline__ u_int
100in_cksum_hdr(const struct ip *ip)
101{
102        register u_int *ap = (u_int *)ip;
103        register u_int sum = *ap++;
104        register u_int tmp;
105                   
106        __asm__ __volatile__("addl  %2@+,%0\n\t"
107                "movel %2@+,%1\n\t"
108                "addxl %1,%0\n\t"
109                "movel %2@+,%1\n\t"
110                "addxl %1,%0\n\t"
111                "movel %2@,%1\n\t"
112                "addxl %1,%0\n\t"
113                "moveq #0,%1\n\t"
114                "addxl %1,%0\n" :
115                "=d" (sum), "=d" (tmp), "=a" (ap) :
116                "0" (sum), "2" (ap), "m"(*ip));
117        sum = (sum & 0xffff) + (sum >> 16);
118        if (sum > 0xffff)
119                sum -= 0xffff;
120        return ~sum & 0xffff;
121}
122
123/*
124 *  Optimized version for the PowerPC family
125 */
126
127#elif (defined(__GNUC__) && (defined(__PPC__) || defined(__ppc__)))
128
129static __inline u_int
130in_cksum_hdr(const struct ip *ip)
131{
132register u_int sum, tmp;
133        __asm__ __volatile__(
134                "       lwz   %0,  0(%2) \n"
135                "       lwz   %1,  4(%2) \n"
136                "       addc  %0, %0, %1 \n"    /* generate carry (XER[CA]) */
137                "       lwz   %1,  8(%2) \n"
138                "       adde  %0, %0, %1 \n"    /* add + generate           */
139                "       lwz   %1, 12(%2) \n"
140                "       adde  %0, %0, %1 \n"
141                "       lwz   %1, 16(%2) \n"
142                "       adde  %0, %0, %1 \n"
143                "       addze %0, %0     \n"    /* mop up XER[CA]           */
144                "       rotlwi %1, %0,16 \n"    /* word-swapped copy in %1  */
145                "       add   %0, %0, %1 \n"    /* see comment below        */
146                "       not   %0, %0     \n"
147                "       srwi  %0, %0, 16 \n"
148                :"=&r"(sum),"=&r"(tmp):"b"(ip), "m"(*ip):"xer"
149        );
150        /* Note: if 'add' generates a carry out of the lower 16 bits
151         *       then this is automatically added to the upper 16 bits
152         *       where the correct result is found. (Stolen from linux.)
153         *        %0 : upper-word  lower-word
154         *     +  %1 : lower-word  upper-word
155         *     =       word-sum    word-sum
156         *                     ^+inter-word-carry
157         */
158        return sum;
159}
160
161static __inline void
162in_cksum_update(struct ip *ip)
163{
164        int __tmpsum;
165        __tmpsum = (int)ntohs(ip->ip_sum) + 256;
166        ip->ip_sum = htons(__tmpsum + (__tmpsum >> 16));
167}
168
169/*
170 *  SPARC Version
171 */
172
173#elif (defined(__GNUC__) && defined(sparc))
174
175static __inline u_int
176in_cksum_hdr(const struct ip *ip)
177{
178   register u_int sum = 0;
179   register u_int tmp_o2;
180   register u_int tmp_o3;
181
182   __asm__ __volatile__ (" \
183     ld [%0], %1 ; \
184     ld [%0+4], %2 ; \
185     ld [%0+8], %3 ; \
186     addcc %1, %2, %1 ; \
187     ld [%0+12], %2 ; \
188     addxcc %1, %3, %1 ; \
189     ld [%0+16], %3 ; \
190     addxcc %1, %2, %1 ; \
191     addxcc %1, %3, %1 ; \
192     set 0x0ffff, %3 ; \
193     srl %1, 16, %2 ; \
194     and %1, %3, %1 ; \
195     addx %1, %2, %1 ; \
196     srl %1, 16, %2 ; \
197     add %1, %2, %1 ; \
198     not %1 ; \
199     and %1, %3, %1 ; \
200    " : "=r" (ip), "=r" (sum), "=r" (tmp_o2), "=r" (tmp_o3)
201      : "0" (ip), "1" (sum), "m"(*ip)
202  );
203  return sum;
204}
205
206#define in_cksum_update(ip) \
207        do { \
208                int __tmpsum; \
209                __tmpsum = (int)ntohs(ip->ip_sum) + 256; \
210                ip->ip_sum = htons(__tmpsum + (__tmpsum >> 16)); \
211        } while(0)
212/*
213 *  Here is the generic, portable, inefficient algorithm.
214 */
215
216#else
217u_int in_cksum_hdr(const struct ip *);
218#define in_cksum_update(ip) \
219        do { \
220                int __tmpsum; \
221                __tmpsum = (int)ntohs(ip->ip_sum) + 256; \
222                ip->ip_sum = htons(__tmpsum + (__tmpsum >> 16)); \
223        } while(0)
224
225#endif
226
227#endif /* _MACHINE_IN_CKSUM_H_ */
Note: See TracBrowser for help on using the repository browser.