1 /*
2  * Copyright (c) 1983 Regents of the University of California.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms are permitted
6  * provided that the above copyright notice and this paragraph are
7  * duplicated in all such forms and that any documentation,
8  * advertising materials, and other materials related to such
9  * distribution and use acknowledge that the software was developed
10  * by the University of California, Berkeley.  The name of the
11  * University may not be used to endorse or promote products derived
12  * from this software without specific prior written permission.
13  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
14  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
15  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
16  */
17 
18 #ifndef lint
19 static char sccsid[] = "@(#)cksum.vax.c	1.4 (Berkeley) 06/18/88";
20 #endif /* not lint */
21 
22 #include <sys/types.h>
23 
24 #define ADD	asm("adwc (r9)+,r8;");
25 
26 /* computes the checksum for ip packets for the VAX */
27 
28 in_cksum(addr, len)
29 u_short *addr;
30 int len;
31 {
32 	register int nleft = len;	/* on vax, (user mode), r11 */
33 #ifndef lint
34 	register int xxx;		/* on vax, (user mode), r10 */
35 #endif not lint
36 	register u_short *w = addr;	/* on vax, known to be r9 */
37 	register int sum = 0;		/* on vax, known to be r8 */
38 
39 	if (((int)w&0x2) && nleft >= 2) {
40 		sum += *w++;
41 		nleft -= 2;
42 	}
43 	while ((nleft -= 32) >= 0) {
44 		asm("clrl r0");		/* clears carry */
45 		ADD; ADD; ADD; ADD; ADD; ADD; ADD; ADD;
46 		asm("adwc $0,r8");
47 	}
48 	nleft += 32;
49 	while ((nleft -= 8) >= 0) {
50 		asm("clrl r0");
51 		ADD; ADD;
52 		asm("adwc $0,r8");
53 	}
54 	nleft += 8;
55 	{ asm("ashl $-16,r8,r0; addw2 r0,r8");
56 	  asm("adwc $0,r8; movzwl r8,r8"); }
57 	while ((nleft -= 2) >= 0) {
58 		asm("movzwl (r9)+,r0; addl2 r0,r8");
59 	}
60 	if (nleft == -1) {
61 		sum += *(u_char *)w;
62 	}
63 
64 	{ asm("ashl $-16,r8,r0; addw2 r0,r8; adwc $0,r8");
65 	  asm("mcoml r8,r8; movzwl r8,r8"); }
66 	return (sum);
67 }
68