-
Notifications
You must be signed in to change notification settings - Fork 9
/
oneC_sum.c
57 lines (48 loc) · 942 Bytes
/
oneC_sum.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include <sys/types.h>
#include <net/gen/oneCsum.h>
u16_t oneC_sum(u16_t prev, void *data, size_t size)
{
u8_t *dptr;
size_t n;
u16_t word;
u32_t sum;
int swap= 0;
sum= prev;
dptr= data;
n= size;
swap= ((size_t) dptr & 1);
if (swap) {
sum= ((sum & 0xFF) << 8) | ((sum & 0xFF00) >> 8);
if (n > 0) {
((u8_t *) &word)[0]= 0;
((u8_t *) &word)[1]= dptr[0];
sum+= (u32_t) word;
dptr+= 1;
n-= 1;
}
}
while (n >= 8) {
sum+= (u32_t) ((u16_t *) dptr)[0]
+ (u32_t) ((u16_t *) dptr)[1]
+ (u32_t) ((u16_t *) dptr)[2]
+ (u32_t) ((u16_t *) dptr)[3];
dptr+= 8;
n-= 8;
}
while (n >= 2) {
sum+= (u32_t) ((u16_t *) dptr)[0];
dptr+= 2;
n-= 2;
}
if (n > 0) {
((u8_t *) &word)[0]= dptr[0];
((u8_t *) &word)[1]= 0;
sum+= (u32_t) word;
}
sum= (sum & 0xFFFF) + (sum >> 16);
if (sum > 0xFFFF) sum++;
if (swap) {
sum= ((sum & 0xFF) << 8) | ((sum & 0xFF00) >> 8);
}
return sum;
}