2 * Copyright 2010 Tilera Corporation. All Rights Reserved.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation, version 2.
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
11 * NON INFRINGEMENT. See the GNU General Public License for
13 * Support code for the main lib/checksum.c.
16 #include <net/checksum.h>
17 #include <linux/module.h>
19 static inline unsigned int longto16(unsigned long x
)
23 ret
= __insn_v2sadu(x
, 0);
24 ret
= __insn_v2sadu(ret
, 0);
26 ret
= __insn_sadh_u(x
, 0);
27 ret
= __insn_sadh_u(ret
, 0);
32 __wsum
do_csum(const unsigned char *buff
, int len
)
35 unsigned long result
= 0;
39 odd
= 1 & (unsigned long) buff
;
41 result
= (*buff
<< 8);
45 count
= len
>> 1; /* nr of 16-bit words.. */
47 if (2 & (unsigned long) buff
) {
48 result
+= *(const unsigned short *)buff
;
53 count
>>= 1; /* nr of 32-bit words.. */
56 if (4 & (unsigned long) buff
) {
57 unsigned int w
= *(const unsigned int *)buff
;
58 result
= __insn_v2sadau(result
, w
, 0);
63 count
>>= 1; /* nr of 64-bit words.. */
67 * This algorithm could wrap around for very
68 * large buffers, but those should be impossible.
70 BUG_ON(count
>= 65530);
73 unsigned long w
= *(const unsigned long *)buff
;
77 result
= __insn_v2sadau(result
, w
, 0);
79 result
= __insn_sadah_u(result
, w
, 0);
84 unsigned int w
= *(const unsigned int *)buff
;
85 result
= __insn_v2sadau(result
, w
, 0);
91 result
+= *(const unsigned short *) buff
;
97 result
= longto16(result
);
99 result
= swab16(result
);