Provide select() on top of epoll()
[akaros.git] / user / iplib / ptclbsum.c
1 /* 
2  * This file is part of the UCB release of Plan 9. It is subject to the license
3  * terms in the LICENSE file found in the top-level directory of this
4  * distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No
5  * part of the UCB release of Plan 9, including this file, may be copied,
6  * modified, propagated, or distributed except according to the terms contained
7  * in the LICENSE file.
8  */
9 #include <stdlib.h>
10
11 #include <stdio.h>
12 #include <parlib/parlib.h>
13 #include <unistd.h>
14 #include <signal.h>
15 #include <iplib/iplib.h>
16
17 static  short   endian  = 1;
18 static  uint8_t*        aendian = (uint8_t*)&endian;
19 #define LITTLE  *aendian
20
21 uint16_t
22 ptclbsum(uint8_t *addr, int len)
23 {
24         uint32_t losum, hisum, mdsum, x;
25         uint32_t t1, t2;
26
27         losum = 0;
28         hisum = 0;
29         mdsum = 0;
30
31         x = 0;
32         if((uintptr_t)addr & 1) {
33                 if(len) {
34                         hisum += addr[0];
35                         len--;
36                         addr++;
37                 }
38                 x = 1;
39         }
40         while(len >= 16) {
41                 t1 = *(uint16_t*)(addr+0);
42                 t2 = *(uint16_t*)(addr+2);      mdsum += t1;
43                 t1 = *(uint16_t*)(addr+4);      mdsum += t2;
44                 t2 = *(uint16_t*)(addr+6);      mdsum += t1;
45                 t1 = *(uint16_t*)(addr+8);      mdsum += t2;
46                 t2 = *(uint16_t*)(addr+10);     mdsum += t1;
47                 t1 = *(uint16_t*)(addr+12);     mdsum += t2;
48                 t2 = *(uint16_t*)(addr+14);     mdsum += t1;
49                 mdsum += t2;
50                 len -= 16;
51                 addr += 16;
52         }
53         while(len >= 2) {
54                 mdsum += *(uint16_t*)addr;
55                 len -= 2;
56                 addr += 2;
57         }
58         if(x) {
59                 if(len)
60                         losum += addr[0];
61                 if(LITTLE)
62                         losum += mdsum;
63                 else
64                         hisum += mdsum;
65         } else {
66                 if(len)
67                         hisum += addr[0];
68                 if(LITTLE)
69                         hisum += mdsum;
70                 else
71                         losum += mdsum;
72         }
73
74         losum += hisum >> 8;
75         losum += (hisum & 0xff) << 8;
76         while(hisum = losum>>16)
77                 losum = hisum + (losum & 0xffff);
78
79         return losum & 0xffff;
80 }