netdev.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] lib/checksum.c: optimize do_csum a bit
@ 2011-07-07 11:18 Ian Abbott
  2011-07-07 11:29 ` David Miller
  0 siblings, 1 reply; 5+ messages in thread
From: Ian Abbott @ 2011-07-07 11:18 UTC (permalink / raw)
  To: netdev; +Cc: Ian Abbott, David S. Miller, Arnd Bergmann, linux-kernel

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 <abbotti@mev.co.uk>
---
 lib/checksum.c |   13 +++++--------
 1 files changed, 5 insertions(+), 8 deletions(-)

diff --git a/lib/checksum.c b/lib/checksum.c
index 0975087..8df2f91 100644
--- a/lib/checksum.c
+++ b/lib/checksum.c
@@ -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)
@@ -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);
 		}
-- 
1.7.6

^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [PATCH] lib/checksum.c: optimize do_csum a bit
  2011-07-07 11:18 [PATCH] lib/checksum.c: optimize do_csum a bit Ian Abbott
@ 2011-07-07 11:29 ` David Miller
  2011-07-07 11:32   ` Ian Abbott
  0 siblings, 1 reply; 5+ messages in thread
From: David Miller @ 2011-07-07 11:29 UTC (permalink / raw)
  To: abbotti; +Cc: netdev, arnd, linux-kernel

From: Ian Abbott <abbotti@mev.co.uk>
Date: Thu, 7 Jul 2011 12:18:49 +0100

> 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 <abbotti@mev.co.uk>

I think you'll overshoot past the end of the buffer when there are
trailing bytes to handle.

The whole reason we need the count variable is to handle those
kinds of cases.

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] lib/checksum.c: optimize do_csum a bit
  2011-07-07 11:29 ` David Miller
@ 2011-07-07 11:32   ` Ian Abbott
  2011-07-07 11:36     ` David Miller
  0 siblings, 1 reply; 5+ messages in thread
From: Ian Abbott @ 2011-07-07 11:32 UTC (permalink / raw)
  To: David Miller
  Cc: Ian Abbott, netdev@vger.kernel.org, arnd@arndb.de,
	linux-kernel@vger.kernel.org

On 07/07/11 12:29, David Miller wrote:
> From: Ian Abbott <abbotti@mev.co.uk>
> Date: Thu, 7 Jul 2011 12:18:49 +0100
> 
>> 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 <abbotti@mev.co.uk>
> 
> I think you'll overshoot past the end of the buffer when there are
> trailing bytes to handle.
> 
> The whole reason we need the count variable is to handle those
> kinds of cases.

I don't think it does.  That's what the & ~3 was for.

-- 
-=( Ian Abbott @ MEV Ltd.    E-mail: <abbotti@mev.co.uk>        )=-
-=( Tel: +44 (0)161 477 1898   FAX: +44 (0)161 718 3587         )=-

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] lib/checksum.c: optimize do_csum a bit
  2011-07-07 11:32   ` Ian Abbott
@ 2011-07-07 11:36     ` David Miller
  2011-07-07 11:52       ` David Miller
  0 siblings, 1 reply; 5+ messages in thread
From: David Miller @ 2011-07-07 11:36 UTC (permalink / raw)
  To: abbotti; +Cc: ian.abbott, netdev, arnd, linux-kernel

From: Ian Abbott <abbotti@mev.co.uk>
Date: Thu, 7 Jul 2011 12:32:45 +0100

> On 07/07/11 12:29, David Miller wrote:
>> From: Ian Abbott <abbotti@mev.co.uk>
>> Date: Thu, 7 Jul 2011 12:18:49 +0100
>> 
>>> 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 <abbotti@mev.co.uk>
>> 
>> I think you'll overshoot past the end of the buffer when there are
>> trailing bytes to handle.
>> 
>> The whole reason we need the count variable is to handle those
>> kinds of cases.
> 
> I don't think it does.  That's what the & ~3 was for.

Aha, yes that indeed makes it work.

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] lib/checksum.c: optimize do_csum a bit
  2011-07-07 11:36     ` David Miller
@ 2011-07-07 11:52       ` David Miller
  0 siblings, 0 replies; 5+ messages in thread
From: David Miller @ 2011-07-07 11:52 UTC (permalink / raw)
  To: abbotti; +Cc: ian.abbott, netdev, arnd, linux-kernel

From: David Miller <davem@davemloft.net>
Date: Thu, 07 Jul 2011 04:36:25 -0700 (PDT)

> From: Ian Abbott <abbotti@mev.co.uk>
> Date: Thu, 7 Jul 2011 12:32:45 +0100
> 
>> On 07/07/11 12:29, David Miller wrote:
>>> From: Ian Abbott <abbotti@mev.co.uk>
>>> Date: Thu, 7 Jul 2011 12:18:49 +0100
>>> 
>>>> 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 <abbotti@mev.co.uk>
>>> 
>>> I think you'll overshoot past the end of the buffer when there are
>>> trailing bytes to handle.
>>> 
>>> The whole reason we need the count variable is to handle those
>>> kinds of cases.
>> 
>> I don't think it does.  That's what the & ~3 was for.
> 
> Aha, yes that indeed makes it work.

I've applied this to net-next-2.6, thanks.

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2011-07-07 11:52 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2011-07-07 11:18 [PATCH] lib/checksum.c: optimize do_csum a bit Ian Abbott
2011-07-07 11:29 ` David Miller
2011-07-07 11:32   ` Ian Abbott
2011-07-07 11:36     ` David Miller
2011-07-07 11:52       ` David Miller

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).