Lindent pass
[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 ptclbsum(uint8_t * addr, int len)
21 {
22         uint32_t losum, hisum, mdsum, x;
23         uint32_t t1, t2;
24
25         losum = 0;
26         hisum = 0;
27         mdsum = 0;
28
29         x = 0;
30         if ((uintptr_t) addr & 1) {
31                 if (len) {
32                         hisum += addr[0];
33                         len--;
34                         addr++;
35                 }
36                 x = 1;
37         }
38         while (len >= 16) {
39                 t1 = *(uint16_t *) (addr + 0);
40                 t2 = *(uint16_t *) (addr + 2);
41                 mdsum += t1;
42                 t1 = *(uint16_t *) (addr + 4);
43                 mdsum += t2;
44                 t2 = *(uint16_t *) (addr + 6);
45                 mdsum += t1;
46                 t1 = *(uint16_t *) (addr + 8);
47                 mdsum += t2;
48                 t2 = *(uint16_t *) (addr + 10);
49                 mdsum += t1;
50                 t1 = *(uint16_t *) (addr + 12);
51                 mdsum += t2;
52                 t2 = *(uint16_t *) (addr + 14);
53                 mdsum += t1;
54                 mdsum += t2;
55                 len -= 16;
56                 addr += 16;
57         }
58         while (len >= 2) {
59                 mdsum += *(uint16_t *) addr;
60                 len -= 2;
61                 addr += 2;
62         }
63         if (x) {
64                 if (len)
65                         losum += addr[0];
66                 if (LITTLE)
67                         losum += mdsum;
68                 else
69                         hisum += mdsum;
70         } else {
71                 if (len)
72                         hisum += addr[0];
73                 if (LITTLE)
74                         hisum += mdsum;
75                 else
76                         losum += mdsum;
77         }
78
79         losum += hisum >> 8;
80         losum += (hisum & 0xff) << 8;
81         while ((hisum = losum >> 16))
82                 losum = hisum + (losum & 0xffff);
83
84         return losum & 0xffff;
85 }