Adds chaninfo()
[akaros.git] / kern / src / net / ptclbsum.c
1 // INFERNO
2 #include <vfs.h>
3 #include <kfs.h>
4 #include <slab.h>
5 #include <kmalloc.h>
6 #include <kref.h>
7 #include <string.h>
8 #include <stdio.h>
9 #include <assert.h>
10 #include <error.h>
11 #include <cpio.h>
12 #include <pmap.h>
13 #include <smp.h>
14 #include <ip.h>
15
16 static  short   endian  = 1;
17 static  uint8_t*        aendian = ( uint8_t *)&endian;
18 #define LITTLE  *aendian
19
20 uint16_t
21 ptclbsum(uint8_t *addr, int len)
22 {
23         uint32_t losum, hisum, mdsum, x;
24         uint32_t t1, t2;
25
26         losum = 0;
27         hisum = 0;
28         mdsum = 0;
29
30         x = 0;
31         if((uintptr_t)addr & 1) {
32                 if(len) {
33                         hisum += addr[0];
34                         len--;
35                         addr++;
36                 }
37                 x = 1;
38         }
39         while(len >= 16) {
40                 t1 = *(uint16_t*)(addr+0);
41                 t2 = *(uint16_t*)(addr+2);      mdsum += t1;
42                 t1 = *(uint16_t*)(addr+4);      mdsum += t2;
43                 t2 = *(uint16_t*)(addr+6);      mdsum += t1;
44                 t1 = *(uint16_t*)(addr+8);      mdsum += t2;
45                 t2 = *(uint16_t*)(addr+10);     mdsum += t1;
46                 t1 = *(uint16_t*)(addr+12);     mdsum += t2;
47                 t2 = *(uint16_t*)(addr+14);     mdsum += t1;
48                 mdsum += t2;
49                 len -= 16;
50                 addr += 16;
51         }
52         while(len >= 2) {
53                 mdsum += *(uint16_t*)addr;
54                 len -= 2;
55                 addr += 2;
56         }
57         if(x) {
58                 if(len)
59                         losum += addr[0];
60                 if(LITTLE)
61                         losum += mdsum;
62                 else
63                         hisum += mdsum;
64         } else {
65                 if(len)
66                         hisum += addr[0];
67                 if(LITTLE)
68                         hisum += mdsum;
69                 else
70                         losum += mdsum;
71         }
72
73         losum += hisum >> 8;
74         losum += (hisum & 0xff) << 8;
75         while((hisum = losum>>16))
76                 losum = hisum + (losum & 0xffff);
77
78         return losum & 0xffff;
79 }