Skip to content

Commit

Permalink
lib/checksum.c: optimize do_csum a bit
Browse files Browse the repository at this point in the history
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: Ian Abbott <[email protected]>
Signed-off-by: David S. Miller <[email protected]>
  • Loading branch information
ian-abbott authored and davem330 committed Jul 7, 2011
1 parent 97bc363 commit be0e1e7
Showing 1 changed file with 5 additions and 8 deletions.
13 changes: 5 additions & 8 deletions lib/checksum.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ static inline unsigned short from32to16(unsigned int x)

static unsigned int do_csum(const unsigned char *buff, int len)
{
int odd, count;
int odd;
unsigned int result = 0;

if (len <= 0)
Expand All @@ -64,25 +64,22 @@ static unsigned int do_csum(const unsigned char *buff, int len)
len--;
buff++;
}
count = len >> 1; /* nr of 16-bit words.. */
if (count) {
if (len >= 2) {
if (2 & (unsigned long) buff) {
result += *(unsigned short *) buff;
count--;
len -= 2;
buff += 2;
}
count >>= 1; /* nr of 32-bit words.. */
if (count) {
if (len >= 4) {
const unsigned char *end = buff + ((unsigned)len & ~3);
unsigned int carry = 0;
do {
unsigned int w = *(unsigned int *) buff;
count--;
buff += 4;
result += carry;
result += w;
carry = (w > result);
} while (count);
} while (buff < end);
result += carry;
result = (result & 0xffff) + (result >> 16);
}
Expand Down

0 comments on commit be0e1e7

Please sign in to comment.