From patchwork Thu Jul 7 11:18:49 2011 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ian Abbott X-Patchwork-Id: 103642 X-Patchwork-Delegate: davem@davemloft.net Return-Path: X-Original-To: patchwork-incoming@ozlabs.org Delivered-To: patchwork-incoming@ozlabs.org Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id 1E7D4B6F18 for ; Thu, 7 Jul 2011 21:28:48 +1000 (EST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1755910Ab1GGL2b (ORCPT ); Thu, 7 Jul 2011 07:28:31 -0400 Received: from mail.mev.co.uk ([62.49.15.74]:38078 "EHLO mail.mev.co.uk" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1755696Ab1GGL23 (ORCPT ); Thu, 7 Jul 2011 07:28:29 -0400 X-Greylist: delayed 557 seconds by postgrey-1.27 at vger.kernel.org; Thu, 07 Jul 2011 07:28:29 EDT Received: from localhost (localhost [127.0.0.1]) by mail.mev.co.uk (Postfix) with ESMTP id 7E17F2600F; Thu, 7 Jul 2011 12:19:11 +0100 (BST) X-Virus-Scanned: Debian amavisd-new at mail.mev.co.uk Received: from mail.mev.co.uk ([127.0.0.1]) by localhost (mantis.mev.local [127.0.0.1]) (amavisd-new, port 10024) with LMTP id Qh3cuGUlLp7J; Thu, 7 Jul 2011 12:19:09 +0100 (BST) Received: from remote.mev.co.uk (mev2008.mev.local [10.0.0.1]) by mail.mev.co.uk (Postfix) with ESMTPS id 292722600C; Thu, 7 Jul 2011 12:19:09 +0100 (BST) Received: from localhost.localdomain (10.0.0.210) by remote.mev.co.uk (10.0.0.1) with Microsoft SMTP Server (TLS) id 8.1.436.0; Thu, 7 Jul 2011 12:19:07 +0100 From: Ian Abbott To: CC: Ian Abbott , "David S. Miller" , Arnd Bergmann , Subject: [PATCH] lib/checksum.c: optimize do_csum a bit Date: Thu, 7 Jul 2011 12:18:49 +0100 Message-ID: <1310037529-30854-1-git-send-email-abbotti@mev.co.uk> X-Mailer: git-send-email 1.7.6 MIME-Version: 1.0 Sender: netdev-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org 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 --- 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); }