public inbox for netdev@vger.kernel.org
 help / color / mirror / Atom feed
From: Eric Dumazet <eric.dumazet@gmail.com>
To: "David S. Miller" <davem@davemloft.net>
Cc: Hagen Paul Pfeifer <hagen@jauu.net>,
	netdev@vger.kernel.org, Changli Gao <xiaosuo@gmail.com>
Subject: [PATCH net-next-2.6] filter: use reciprocal divide
Date: Fri, 19 Nov 2010 09:04:46 +0100	[thread overview]
Message-ID: <1290153886.29509.10.camel@edumazet-laptop> (raw)
In-Reply-To: <1290153398.29509.7.camel@edumazet-laptop>

At compile time, we can replace the DIV_K instruction (divide by a
constant value) by a reciprocal divide.

At exec time, the expensive divide is replaced by a multiply, a less
expensive operation on most processors.

Signed-off-by: Eric Dumazet <eric.dumazet@gmail.com>
---
 net/core/filter.c |    4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/net/core/filter.c b/net/core/filter.c
index a1edb5d..13853c7 100644
--- a/net/core/filter.c
+++ b/net/core/filter.c
@@ -37,6 +37,7 @@
 #include <asm/uaccess.h>
 #include <asm/unaligned.h>
 #include <linux/filter.h>
+#include <linux/reciprocal_div.h>
 
 enum {
 	BPF_S_RET_K = 1,
@@ -202,7 +203,7 @@ unsigned int sk_run_filter(struct sk_buff *skb, struct sock_filter *filter, int
 			A /= X;
 			continue;
 		case BPF_S_ALU_DIV_K:
-			A /= f_k;
+			A = reciprocal_divide(A, f_k);
 			continue;
 		case BPF_S_ALU_AND_X:
 			A &= X;
@@ -503,6 +504,7 @@ int sk_chk_filter(struct sock_filter *filter, int flen)
 			/* check for division by zero */
 			if (ftest->k == 0)
 				return -EINVAL;
+			ftest->k = reciprocal_value(ftest->k);
 			break;
 		case BPF_S_LD_MEM:
 		case BPF_S_LDX_MEM:



  reply	other threads:[~2010-11-19  8:04 UTC|newest]

Thread overview: 27+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-11-19  2:04 [PATCH] net: reduce the lines of code Changli Gao
2010-11-19  6:35 ` Hagen Paul Pfeifer
2010-11-19  7:17   ` Changli Gao
2010-11-19  7:51     ` Eric Dumazet
2010-11-19  7:56       ` [PATCH net-next-2.6] filter: cleanup codes[] init Eric Dumazet
2010-11-19  8:04         ` Eric Dumazet [this message]
2010-11-19  8:18           ` [PATCH net-next-2.6] filter: use reciprocal divide Changli Gao
2010-11-19 18:07             ` David Miller
2010-11-19  8:38         ` [PATCH net-next-2.6] filter: cleanup codes[] init Changli Gao
2010-11-19  9:54           ` Eric Dumazet
2010-11-19 11:17             ` [PATCH net-next-2.6] filter: optimize sk_run_filter Eric Dumazet
2010-11-19 12:32               ` Changli Gao
2010-11-19 12:57                 ` Changli Gao
2010-11-19 13:16                   ` [PATCH net-next-2.6 v2] " Eric Dumazet
2010-11-19 14:13                     ` Changli Gao
2010-11-19 17:52                       ` David Miller
2010-11-19 14:17                     ` Tetsuo Handa
2010-11-19 14:35                       ` Eric Dumazet
2010-11-19 16:21               ` [PATCH net-next-2.6] " David Miller
2010-11-19 16:55                 ` Eric Dumazet
2010-11-19 17:05                   ` David Miller
2010-11-19 17:15                     ` Stephen Hemminger
2010-11-19 17:21                       ` David Miller
2010-11-19 17:16                     ` Eric Dumazet
2010-11-19 17:25                       ` David Miller
2010-11-19 12:21             ` [PATCH net-next-2.6] filter: cleanup codes[] init Changli Gao
2010-11-19 18:07               ` David Miller

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1290153886.29509.10.camel@edumazet-laptop \
    --to=eric.dumazet@gmail.com \
    --cc=davem@davemloft.net \
    --cc=hagen@jauu.net \
    --cc=netdev@vger.kernel.org \
    --cc=xiaosuo@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox