OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [trunk/] [linux/] [linux-2.4/] [arch/] [alpha/] [lib/] [checksum.c] - Blame information for rev 1765

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 1275 phoenix
/*
2
 * arch/alpha/lib/checksum.c
3
 *
4
 * This file contains network checksum routines that are better done
5
 * in an architecture-specific manner due to speed..
6
 * Comments in other versions indicate that the algorithms are from RFC1071
7
 *
8
 * accellerated versions (and 21264 assembly versions ) contributed by
9
 *      Rick Gorton     <rick.gorton@alpha-processor.com>
10
 */
11
 
12
#include <linux/string.h>
13
 
14
#include <asm/byteorder.h>
15
 
16
static inline unsigned short from64to16(unsigned long x)
17
{
18
        /* Using extract instructions is a bit more efficient
19
           than the original shift/bitmask version.  */
20
 
21
        union {
22
                unsigned long   ul;
23
                unsigned int    ui[2];
24
                unsigned short  us[4];
25
        } in_v, tmp_v, out_v;
26
 
27
        in_v.ul = x;
28
        tmp_v.ul = (unsigned long) in_v.ui[0] + (unsigned long) in_v.ui[1];
29
 
30
        /* Since the bits of tmp_v.sh[3] are going to always be zero,
31
           we don't have to bother to add that in.  */
32
        out_v.ul = (unsigned long) tmp_v.us[0] + (unsigned long) tmp_v.us[1]
33
                        + (unsigned long) tmp_v.us[2];
34
 
35
        /* Similarly, out_v.us[2] is always zero for the final add.  */
36
        return out_v.us[0] + out_v.us[1];
37
}
38
 
39
/*
40
 * computes the checksum of the TCP/UDP pseudo-header
41
 * returns a 16-bit checksum, already complemented.
42
 */
43
unsigned short int csum_tcpudp_magic(unsigned long saddr,
44
                                   unsigned long daddr,
45
                                   unsigned short len,
46
                                   unsigned short proto,
47
                                   unsigned int sum)
48
{
49
        return ~from64to16(saddr + daddr + sum +
50
                ((unsigned long) ntohs(len) << 16) +
51
                ((unsigned long) proto << 8));
52
}
53
 
54
unsigned int csum_tcpudp_nofold(unsigned long saddr,
55
                                   unsigned long daddr,
56
                                   unsigned short len,
57
                                   unsigned short proto,
58
                                   unsigned int sum)
59
{
60
        unsigned long result;
61
 
62
        result = (saddr + daddr + sum +
63
                  ((unsigned long) ntohs(len) << 16) +
64
                  ((unsigned long) proto << 8));
65
 
66
        /* Fold down to 32-bits so we don't loose in the typedef-less
67
           network stack.  */
68
        /* 64 to 33 */
69
        result = (result & 0xffffffff) + (result >> 32);
70
        /* 33 to 32 */
71
        result = (result & 0xffffffff) + (result >> 32);
72
        return result;
73
}
74
 
75
/*
76
 * Do a 64-bit checksum on an arbitrary memory area..
77
 *
78
 * This isn't a great routine, but it's not _horrible_ either. The
79
 * inner loop could be unrolled a bit further, and there are better
80
 * ways to do the carry, but this is reasonable.
81
 */
82
static inline unsigned long do_csum(const unsigned char * buff, int len)
83
{
84
        int odd, count;
85
        unsigned long result = 0;
86
 
87
        if (len <= 0)
88
                goto out;
89
        odd = 1 & (unsigned long) buff;
90
        if (odd) {
91
                result = *buff << 8;
92
                len--;
93
                buff++;
94
        }
95
        count = len >> 1;               /* nr of 16-bit words.. */
96
        if (count) {
97
                if (2 & (unsigned long) buff) {
98
                        result += *(unsigned short *) buff;
99
                        count--;
100
                        len -= 2;
101
                        buff += 2;
102
                }
103
                count >>= 1;            /* nr of 32-bit words.. */
104
                if (count) {
105
                        if (4 & (unsigned long) buff) {
106
                                result += *(unsigned int *) buff;
107
                                count--;
108
                                len -= 4;
109
                                buff += 4;
110
                        }
111
                        count >>= 1;    /* nr of 64-bit words.. */
112
                        if (count) {
113
                                unsigned long carry = 0;
114
                                do {
115
                                        unsigned long w = *(unsigned long *) buff;
116
                                        count--;
117
                                        buff += 8;
118
                                        result += carry;
119
                                        result += w;
120
                                        carry = (w > result);
121
                                } while (count);
122
                                result += carry;
123
                                result = (result & 0xffffffff) + (result >> 32);
124
                        }
125
                        if (len & 4) {
126
                                result += *(unsigned int *) buff;
127
                                buff += 4;
128
                        }
129
                }
130
                if (len & 2) {
131
                        result += *(unsigned short *) buff;
132
                        buff += 2;
133
                }
134
        }
135
        if (len & 1)
136
                result += *buff;
137
        result = from64to16(result);
138
        if (odd)
139
                result = ((result >> 8) & 0xff) | ((result & 0xff) << 8);
140
out:
141
        return result;
142
}
143
 
144
/*
145
 *      This is a version of ip_compute_csum() optimized for IP headers,
146
 *      which always checksum on 4 octet boundaries.
147
 */
148
unsigned short ip_fast_csum(unsigned char * iph, unsigned int ihl)
149
{
150
        return ~do_csum(iph,ihl*4);
151
}
152
 
153
/*
154
 * computes the checksum of a memory block at buff, length len,
155
 * and adds in "sum" (32-bit)
156
 *
157
 * returns a 32-bit number suitable for feeding into itself
158
 * or csum_tcpudp_magic
159
 *
160
 * this function must be called with even lengths, except
161
 * for the last fragment, which may be odd
162
 *
163
 * it's best to have buff aligned on a 32-bit boundary
164
 */
165
unsigned int csum_partial(const unsigned char * buff, int len, unsigned int sum)
166
{
167
        unsigned long result = do_csum(buff, len);
168
 
169
        /* add in old sum, and carry.. */
170
        result += sum;
171
        /* 32+c bits -> 32 bits */
172
        result = (result & 0xffffffff) + (result >> 32);
173
        return result;
174
}
175
 
176
/*
177
 * this routine is used for miscellaneous IP-like checksums, mainly
178
 * in icmp.c
179
 */
180
unsigned short ip_compute_csum(unsigned char * buff, int len)
181
{
182
        return ~from64to16(do_csum(buff,len));
183
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.