Commit be0e1e78 authored by Ian Abbott's avatar Ian Abbott Committed by David S. Miller

lib/checksum.c: optimize do_csum a bit

Reduce the number of variables modified by the loop in do_csum() by 1,
which seems like a good idea.  On Nios II (a RISC CPU with 3-operand
instruction set) it reduces the loop from 7 to 6 instructions, including
the conditional branch.
Signed-off-by: default avatarIan Abbott <abbotti@mev.co.uk>
Signed-off-by: default avatarDavid S. Miller <davem@davemloft.net>
parent 97bc3633
...@@ -49,7 +49,7 @@ static inline unsigned short from32to16(unsigned int x) ...@@ -49,7 +49,7 @@ static inline unsigned short from32to16(unsigned int x)
static unsigned int do_csum(const unsigned char *buff, int len) static unsigned int do_csum(const unsigned char *buff, int len)
{ {
int odd, count; int odd;
unsigned int result = 0; unsigned int result = 0;
if (len <= 0) if (len <= 0)
...@@ -64,25 +64,22 @@ static unsigned int do_csum(const unsigned char *buff, int len) ...@@ -64,25 +64,22 @@ static unsigned int do_csum(const unsigned char *buff, int len)
len--; len--;
buff++; buff++;
} }
count = len >> 1; /* nr of 16-bit words.. */ if (len >= 2) {
if (count) {
if (2 & (unsigned long) buff) { if (2 & (unsigned long) buff) {
result += *(unsigned short *) buff; result += *(unsigned short *) buff;
count--;
len -= 2; len -= 2;
buff += 2; buff += 2;
} }
count >>= 1; /* nr of 32-bit words.. */ if (len >= 4) {
if (count) { const unsigned char *end = buff + ((unsigned)len & ~3);
unsigned int carry = 0; unsigned int carry = 0;
do { do {
unsigned int w = *(unsigned int *) buff; unsigned int w = *(unsigned int *) buff;
count--;
buff += 4; buff += 4;
result += carry; result += carry;
result += w; result += w;
carry = (w > result); carry = (w > result);
} while (count); } while (buff < end);
result += carry; result += carry;
result = (result & 0xffff) + (result >> 16); result = (result & 0xffff) + (result >> 16);
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment