xref: /freebsd/sys/i386/include/in_cksum.h (revision 97f588a78b037f0da3acfcab2a68a463079d9f14)
197f588a7SGarrett Wollman /*-
297f588a7SGarrett Wollman  * Copyright (c) 1990 The Regents of the University of California.
397f588a7SGarrett Wollman  * All rights reserved.
497f588a7SGarrett Wollman  *
597f588a7SGarrett Wollman  * Redistribution and use in source and binary forms, with or without
697f588a7SGarrett Wollman  * modification, are permitted provided that the following conditions
797f588a7SGarrett Wollman  * are met:
897f588a7SGarrett Wollman  * 1. Redistributions of source code must retain the above copyright
997f588a7SGarrett Wollman  *    notice, this list of conditions and the following disclaimer.
1097f588a7SGarrett Wollman  * 2. Redistributions in binary form must reproduce the above copyright
1197f588a7SGarrett Wollman  *    notice, this list of conditions and the following disclaimer in the
1297f588a7SGarrett Wollman  *    documentation and/or other materials provided with the distribution.
1397f588a7SGarrett Wollman  * 3. All advertising materials mentioning features or use of this software
1497f588a7SGarrett Wollman  *    must display the following acknowledgement:
1597f588a7SGarrett Wollman  *	This product includes software developed by the University of
1697f588a7SGarrett Wollman  *	California, Berkeley and its contributors.
1797f588a7SGarrett Wollman  * 4. Neither the name of the University nor the names of its contributors
1897f588a7SGarrett Wollman  *    may be used to endorse or promote products derived from this software
1997f588a7SGarrett Wollman  *    without specific prior written permission.
2097f588a7SGarrett Wollman  *
2197f588a7SGarrett Wollman  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2297f588a7SGarrett Wollman  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2397f588a7SGarrett Wollman  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2497f588a7SGarrett Wollman  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2597f588a7SGarrett Wollman  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2697f588a7SGarrett Wollman  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2797f588a7SGarrett Wollman  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2897f588a7SGarrett Wollman  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2997f588a7SGarrett Wollman  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3097f588a7SGarrett Wollman  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3197f588a7SGarrett Wollman  * SUCH DAMAGE.
3297f588a7SGarrett Wollman  *
3397f588a7SGarrett Wollman  *	from tahoe:	in_cksum.c	1.2	86/01/05
3497f588a7SGarrett Wollman  *	from:		@(#)in_cksum.c	1.3 (Berkeley) 1/19/91
3597f588a7SGarrett Wollman  *	from: Id: in_cksum.c,v 1.8 1995/12/03 18:35:19 bde Exp
3697f588a7SGarrett Wollman  *	$Id$
3797f588a7SGarrett Wollman  */
3897f588a7SGarrett Wollman 
3997f588a7SGarrett Wollman #ifndef _MACHINE_IN_CKSUM_H_
4097f588a7SGarrett Wollman #define	_MACHINE_IN_CKSUM_H_	1
4197f588a7SGarrett Wollman 
4297f588a7SGarrett Wollman #include <sys/cdefs.h>
4397f588a7SGarrett Wollman 
4497f588a7SGarrett Wollman /*
4597f588a7SGarrett Wollman  * It it useful to have an Internet checksum routine which is inlineable
4697f588a7SGarrett Wollman  * and optimized specifically for the task of computing IP header checksums
4797f588a7SGarrett Wollman  * in the normal case (where there are no options and the header length is
4897f588a7SGarrett Wollman  * therefore always exactly five 32-bit words.
4997f588a7SGarrett Wollman  */
5097f588a7SGarrett Wollman #ifdef __GNUC__
5197f588a7SGarrett Wollman static __inline u_int
5297f588a7SGarrett Wollman in_cksum_hdr(const struct ip *ip)
5397f588a7SGarrett Wollman {
5497f588a7SGarrett Wollman 	register u_int sum = 0;
5597f588a7SGarrett Wollman 
5697f588a7SGarrett Wollman #define ADD(n)	__asm("addl " #n "(%2), %0" : "=r" (sum) : "0" (sum), "r" (ip))
5797f588a7SGarrett Wollman #define ADDC(n)	__asm("adcl " #n "(%2), %0" : "=r" (sum) : "0" (sum), "r" (ip))
5897f588a7SGarrett Wollman #define MOP	__asm("adcl         $0, %0" : "=r" (sum) : "0" (sum))
5997f588a7SGarrett Wollman 
6097f588a7SGarrett Wollman 	ADD(0);
6197f588a7SGarrett Wollman 	ADDC(4);
6297f588a7SGarrett Wollman 	ADDC(8);
6397f588a7SGarrett Wollman 	ADDC(12);
6497f588a7SGarrett Wollman 	ADDC(16);
6597f588a7SGarrett Wollman 	MOP;
6697f588a7SGarrett Wollman 	sum = (sum & 0xffff) + (sum >> 16);
6797f588a7SGarrett Wollman 	if (sum > 0xffff)
6897f588a7SGarrett Wollman 		sum -= 0xffff;
6997f588a7SGarrett Wollman 
7097f588a7SGarrett Wollman 	return ~sum & 0xffff;
7197f588a7SGarrett Wollman }
7297f588a7SGarrett Wollman #else
7397f588a7SGarrett Wollman u_int in_cksum_hdr __P((const struct ip *));
7497f588a7SGarrett Wollman #endif
7597f588a7SGarrett Wollman 
7697f588a7SGarrett Wollman #endif /* _MACHINE_IN_CKSUM_H_ */
77