2 * arch/alpha/lib/checksum.c
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
8 * accelerated versions (and 21264 assembly versions ) contributed by
9 * Rick Gorton <rick.gorton@alpha-processor.com>
12 #include <linux/module.h>
13 #include <linux/string.h>
15 #include <asm/byteorder.h>
17 static inline unsigned short from64to16(unsigned long x)
19 /* Using extract instructions is a bit more efficient
20 than the original shift/bitmask version. */
29 tmp_v.ul = (unsigned long) in_v.ui[0] + (unsigned long) in_v.ui[1];
31 /* Since the bits of tmp_v.sh[3] are going to always be zero,
32 we don't have to bother to add that in. */
33 out_v.ul = (unsigned long) tmp_v.us[0] + (unsigned long) tmp_v.us[1]
34 + (unsigned long) tmp_v.us[2];
36 /* Similarly, out_v.us[2] is always zero for the final add. */
37 return out_v.us[0] + out_v.us[1];
41 * computes the checksum of the TCP/UDP pseudo-header
42 * returns a 16-bit checksum, already complemented.
44 __sum16 csum_tcpudp_magic(__be32 saddr, __be32 daddr,
49 return (__force __sum16)~from64to16(
50 (__force u64)saddr + (__force u64)daddr +
51 (__force u64)sum + ((len + proto) << 8));
53 EXPORT_SYMBOL(csum_tcpudp_magic);
55 __wsum csum_tcpudp_nofold(__be32 saddr, __be32 daddr,
62 result = (__force u64)saddr + (__force u64)daddr +
63 (__force u64)sum + ((len + proto) << 8);
65 /* Fold down to 32-bits so we don't lose in the typedef-less
68 result = (result & 0xffffffff) + (result >> 32);
70 result = (result & 0xffffffff) + (result >> 32);
71 return (__force __wsum)result;
73 EXPORT_SYMBOL(csum_tcpudp_nofold);
76 * Do a 64-bit checksum on an arbitrary memory area..
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.
82 static inline unsigned long do_csum(const unsigned char * buff, int len)
85 unsigned long result = 0;
89 odd = 1 & (unsigned long) buff;
95 count = len >> 1; /* nr of 16-bit words.. */
97 if (2 & (unsigned long) buff) {
98 result += *(unsigned short *) buff;
103 count >>= 1; /* nr of 32-bit words.. */
105 if (4 & (unsigned long) buff) {
106 result += *(unsigned int *) buff;
111 count >>= 1; /* nr of 64-bit words.. */
113 unsigned long carry = 0;
115 unsigned long w = *(unsigned long *) buff;
120 carry = (w > result);
123 result = (result & 0xffffffff) + (result >> 32);
126 result += *(unsigned int *) buff;
131 result += *(unsigned short *) buff;
137 result = from64to16(result);
139 result = ((result >> 8) & 0xff) | ((result & 0xff) << 8);
145 * This is a version of ip_compute_csum() optimized for IP headers,
146 * which always checksum on 4 octet boundaries.
148 __sum16 ip_fast_csum(const void *iph, unsigned int ihl)
150 return (__force __sum16)~do_csum(iph,ihl*4);
152 EXPORT_SYMBOL(ip_fast_csum);
155 * computes the checksum of a memory block at buff, length len,
156 * and adds in "sum" (32-bit)
158 * returns a 32-bit number suitable for feeding into itself
159 * or csum_tcpudp_magic
161 * this function must be called with even lengths, except
162 * for the last fragment, which may be odd
164 * it's best to have buff aligned on a 32-bit boundary
166 __wsum csum_partial(const void *buff, int len, __wsum sum)
168 unsigned long result = do_csum(buff, len);
170 /* add in old sum, and carry.. */
171 result += (__force u32)sum;
172 /* 32+c bits -> 32 bits */
173 result = (result & 0xffffffff) + (result >> 32);
174 return (__force __wsum)result;
177 EXPORT_SYMBOL(csum_partial);
180 * this routine is used for miscellaneous IP-like checksums, mainly
183 __sum16 ip_compute_csum(const void *buff, int len)
185 return (__force __sum16)~from64to16(do_csum(buff,len));
187 EXPORT_SYMBOL(ip_compute_csum);