Linux 4.8-rc8
[linux/fpc-iii.git] / arch / alpha / lib / checksum.c
blob377f9e34eb9709631e50aa20ce87417ebdeac3a8
1 /*
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. */
22 union {
23 unsigned long ul;
24 unsigned int ui[2];
25 unsigned short us[4];
26 } in_v, tmp_v, out_v;
28 in_v.ul = x;
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,
45 __u32 len, __u8 proto, __wsum sum)
47 return (__force __sum16)~from64to16(
48 (__force u64)saddr + (__force u64)daddr +
49 (__force u64)sum + ((len + proto) << 8));
52 __wsum csum_tcpudp_nofold(__be32 saddr, __be32 daddr,
53 __u32 len, __u8 proto, __wsum sum)
55 unsigned long result;
57 result = (__force u64)saddr + (__force u64)daddr +
58 (__force u64)sum + ((len + proto) << 8);
60 /* Fold down to 32-bits so we don't lose in the typedef-less
61 network stack. */
62 /* 64 to 33 */
63 result = (result & 0xffffffff) + (result >> 32);
64 /* 33 to 32 */
65 result = (result & 0xffffffff) + (result >> 32);
66 return (__force __wsum)result;
68 EXPORT_SYMBOL(csum_tcpudp_nofold);
71 * Do a 64-bit checksum on an arbitrary memory area..
73 * This isn't a great routine, but it's not _horrible_ either. The
74 * inner loop could be unrolled a bit further, and there are better
75 * ways to do the carry, but this is reasonable.
77 static inline unsigned long do_csum(const unsigned char * buff, int len)
79 int odd, count;
80 unsigned long result = 0;
82 if (len <= 0)
83 goto out;
84 odd = 1 & (unsigned long) buff;
85 if (odd) {
86 result = *buff << 8;
87 len--;
88 buff++;
90 count = len >> 1; /* nr of 16-bit words.. */
91 if (count) {
92 if (2 & (unsigned long) buff) {
93 result += *(unsigned short *) buff;
94 count--;
95 len -= 2;
96 buff += 2;
98 count >>= 1; /* nr of 32-bit words.. */
99 if (count) {
100 if (4 & (unsigned long) buff) {
101 result += *(unsigned int *) buff;
102 count--;
103 len -= 4;
104 buff += 4;
106 count >>= 1; /* nr of 64-bit words.. */
107 if (count) {
108 unsigned long carry = 0;
109 do {
110 unsigned long w = *(unsigned long *) buff;
111 count--;
112 buff += 8;
113 result += carry;
114 result += w;
115 carry = (w > result);
116 } while (count);
117 result += carry;
118 result = (result & 0xffffffff) + (result >> 32);
120 if (len & 4) {
121 result += *(unsigned int *) buff;
122 buff += 4;
125 if (len & 2) {
126 result += *(unsigned short *) buff;
127 buff += 2;
130 if (len & 1)
131 result += *buff;
132 result = from64to16(result);
133 if (odd)
134 result = ((result >> 8) & 0xff) | ((result & 0xff) << 8);
135 out:
136 return result;
140 * This is a version of ip_compute_csum() optimized for IP headers,
141 * which always checksum on 4 octet boundaries.
143 __sum16 ip_fast_csum(const void *iph, unsigned int ihl)
145 return (__force __sum16)~do_csum(iph,ihl*4);
149 * computes the checksum of a memory block at buff, length len,
150 * and adds in "sum" (32-bit)
152 * returns a 32-bit number suitable for feeding into itself
153 * or csum_tcpudp_magic
155 * this function must be called with even lengths, except
156 * for the last fragment, which may be odd
158 * it's best to have buff aligned on a 32-bit boundary
160 __wsum csum_partial(const void *buff, int len, __wsum sum)
162 unsigned long result = do_csum(buff, len);
164 /* add in old sum, and carry.. */
165 result += (__force u32)sum;
166 /* 32+c bits -> 32 bits */
167 result = (result & 0xffffffff) + (result >> 32);
168 return (__force __wsum)result;
171 EXPORT_SYMBOL(csum_partial);
174 * this routine is used for miscellaneous IP-like checksums, mainly
175 * in icmp.c
177 __sum16 ip_compute_csum(const void *buff, int len)
179 return (__force __sum16)~from64to16(do_csum(buff,len));