1 /* SPDX-License-Identifier: GPL-2.0-or-later */
3 * INET An implementation of the TCP/IP protocol suite for the LINUX
4 * operating system. INET is implemented using the BSD Socket
5 * interface as the means of communication with the user level.
7 * IP/TCP/UDP checksumming routines
9 * Authors: Jorge Cwik, <jorge@laser.satlink.net>
10 * Arnt Gulbrandsen, <agulbra@nvg.unit.no>
11 * Tom May, <ftom@netcom.com>
12 * Pentium Pro/II routines:
13 * Alexander Kjeldaas <astor@guardian.no>
14 * Finn Arne Gangstad <finnag@guardian.no>
15 * Lots of code moved from tcp.c and ip.c; see those files
18 * Changes: Ingo Molnar, converted csum_partial_copy() to 2.1 exception
20 * Andi Kleen, add zeroing on error
21 * converted to pure assembler
24 #include <asm/errno.h>
26 #include <asm/export.h>
29 * computes a partial checksum, e.g. for TCP/UDP fragments
33 unsigned int csum_partial(const unsigned char * buff, int len, unsigned int sum)
40 #ifndef CONFIG_X86_USE_PPRO_CHECKSUM
43 * Experiments with Ethernet and SLIP connections show that buff
44 * is aligned on either a 2-byte or 4-byte boundary. We get at
45 * least a twofold speedup on 486 and Pentium if it is 4-byte aligned.
46 * Fortunately, it is easy to convert 2-byte alignment to 4-byte
47 * alignment for the unrolled loop.
52 movl 20(%esp),%eax # Function arg: unsigned int sum
53 movl 16(%esp),%ecx # Function arg: int len
54 movl 12(%esp),%esi # Function arg: unsigned char *buff
55 testl $2, %esi # Check alignment.
56 jz 2f # Jump if alignment is ok.
57 subl $2, %ecx # Alignment uses up two bytes.
58 jae 1f # Jump if we had at least two bytes.
59 addl $2, %ecx # ecx was < 2. Deal with it.
93 shrl $2, %edx # This clears CF
117 /* Version for PentiumII/PPro */
122 movl 20(%esp),%eax # Function arg: unsigned int sum
123 movl 16(%esp),%ecx # Function arg: int len
124 movl 12(%esp),%esi # Function arg: const unsigned char *buf
136 lea 45f(%ebx,%ebx,2), %ebx
140 # Handle 2-byte-aligned regions
149 movzbl (%esi),%ebx # csumming 1 byte, 2-aligned
154 addw (%esi), %ax # csumming 2 bytes, 2-aligned
159 addl -128(%esi), %eax
160 adcl -124(%esi), %eax
161 adcl -120(%esi), %eax
162 adcl -116(%esi), %eax
163 adcl -112(%esi), %eax
164 adcl -108(%esi), %eax
165 adcl -104(%esi), %eax
166 adcl -100(%esi), %eax
200 # Handle the last 1-3 bytes without jumping
201 notl %ecx # 1->2, 2->1, 3->0, higher bits are masked
202 movl $0xffffff,%ebx # by the shll and shrl instructions
205 andl -128(%esi),%ebx # esi is 4-aligned so should be ok
214 EXPORT_SYMBOL(csum_partial)