* Copyright (c) 1982, 1988 Regents of the University of California.
* Redistribution and use in source and binary forms are permitted
* provided that the above copyright notice and this paragraph are
* duplicated in all such forms and that any documentation,
* advertising materials, and other materials related to such
* distribution and use acknowledge that the software was developed
* by the University of California, Berkeley. The name of the
* University may not be used to endorse or promote products derived
* from this software without specific prior written permission.
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
* WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
* @(#)ns_cksum.c 7.3 (Berkeley) 6/29/88
* Checksum routine for Network Systems Protocol Packets (Big-Endian).
* This routine is very heavily used in the network
* code and should be modified for each CPU to be as fast as possible.
#define ADDCARRY(x) { if ((x) > 65535) (x) -= 65535; }
#define FOLD(x) {l_util.l = (x); (x) = l_util.s[0] + l_util.s[1]; ADDCARRY(x);}
for (;m
&& len
; m
= m
->m_next
) {
* Each trip around loop adds in
* word from one mbuf segment.
* There is a byte left from the last segment;
* ones-complement add it into the checksum.
sum
+= *(u_char
*)w
; /* Big-Endian, else << 8 */
w
= (u_short
*)(1 + (char *)w
);
* We can do a 16 bit ones complement sum using
* 32 bit arithmetic registers for adding,
* with carries from the low added
* into the high (by normal carry-chaining)
* so long as we fold back before 16 carries have occured.
/* -DTINY reduces the size from 1250 to 550, but slows it down by 22% */
while ((mlen
-= 32) >= 0) {
sum
+= w
[0]; sum
+= sum
; sum
+= w
[1]; sum
+= sum
;
sum
+= w
[2]; sum
+= sum
; sum
+= w
[3]; sum
+= sum
;
sum
+= w
[4]; sum
+= sum
; sum
+= w
[5]; sum
+= sum
;
sum
+= w
[6]; sum
+= sum
; sum
+= w
[7]; sum
+= sum
;
sum
+= w
[8]; sum
+= sum
; sum
+= w
[9]; sum
+= sum
;
sum
+= w
[10]; sum
+= sum
; sum
+= w
[11]; sum
+= sum
;
sum
+= w
[12]; sum
+= sum
; sum
+= w
[13]; sum
+= sum
;
sum
+= w
[14]; sum
+= sum
; sum
+= w
[15]; sum
+= sum
;
while ((mlen
-= 8) >= 0) {
sum
+= w
[0]; sum
+= sum
; sum
+= w
[1]; sum
+= sum
;
sum
+= w
[2]; sum
+= sum
; sum
+= w
[3]; sum
+= sum
;
while ((mlen
-= 2) >= 0) {
/* Big-Endian; else reverse ww and vv */
#define ww(n) (((u_char *)w)[n + n + 1])
#define vv(n) (((u_char *)w)[n + n])
while ((mlen
-= 32) >= 0) {
sum
+= ww(0); sum
+= sum
; sum
+= ww(1); sum
+= sum
;
sum
+= ww(2); sum
+= sum
; sum
+= ww(3); sum
+= sum
;
sum
+= ww(4); sum
+= sum
; sum
+= ww(5); sum
+= sum
;
sum
+= ww(6); sum
+= sum
; sum
+= ww(7); sum
+= sum
;
sum
+= ww(8); sum
+= sum
; sum
+= ww(9); sum
+= sum
;
sum
+= ww(10); sum
+= sum
; sum
+= ww(11); sum
+= sum
;
sum
+= ww(12); sum
+= sum
; sum
+= ww(13); sum
+= sum
;
sum
+= ww(14); sum
+= sum
; sum
+= ww(15); sum
+= sum
;
sum2
+= vv(0); sum2
+= sum2
; sum2
+= vv(1); sum2
+= sum2
;
sum2
+= vv(2); sum2
+= sum2
; sum2
+= vv(3); sum2
+= sum2
;
sum2
+= vv(4); sum2
+= sum2
; sum2
+= vv(5); sum2
+= sum2
;
sum2
+= vv(6); sum2
+= sum2
; sum2
+= vv(7); sum2
+= sum2
;
sum2
+= vv(8); sum2
+= sum2
; sum2
+= vv(9); sum2
+= sum2
;
sum2
+= vv(10); sum2
+= sum2
; sum2
+= vv(11); sum2
+= sum2
;
sum2
+= vv(12); sum2
+= sum2
; sum2
+= vv(13); sum2
+= sum2
;
sum2
+= vv(14); sum2
+= sum2
; sum2
+= vv(15); sum2
+= sum2
;
while ((mlen
-= 8) >= 0) {
sum
+= ww(0); sum
+= sum
; sum
+= ww(1); sum
+= sum
;
sum
+= ww(2); sum
+= sum
; sum
+= ww(3); sum
+= sum
;
sum2
+= vv(0); sum2
+= sum2
; sum2
+= vv(1); sum2
+= sum2
;
sum2
+= vv(2); sum2
+= sum2
; sum2
+= vv(3); sum2
+= sum2
;
while ((mlen
-= 2) >= 0) {
sum
+= ww(0); sum
+= sum
;
sum2
+= vv(0); sum2
+= sum2
;
sum
+= *(u_char
*)w
<< 8; /* Big-Endian, else no << 8 */
/* We had an odd number of bytes to sum; assume a garbage
byte of zero and clean up */
* sum has already been kept to low sixteen bits.
* just examine result and exit.