From patchwork Tue Jul 20 09:03:09 2010 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sven Eckelmann X-Patchwork-Id: 335 Return-Path: Received: from mail.gmx.net (mail.gmx.net [213.165.64.20]) by open-mesh.net (Postfix) with SMTP id 88B2A1543FD for ; Tue, 20 Jul 2010 11:04:05 +0200 (CEST) Received: (qmail invoked by alias); 20 Jul 2010 09:04:04 -0000 Received: from unknown (EHLO sven-desktop.lazhur.ath.cx) [89.246.211.23] by mail.gmx.net (mp056) with SMTP; 20 Jul 2010 11:04:04 +0200 X-Authenticated: #15668376 X-Provags-ID: V01U2FsdGVkX1/SGR5OM5PSmZFzDfCz0Gp3E4A3Qf5Vd3TMpkEodS KtQK46oQbbhoDq From: Sven Eckelmann To: b.a.t.m.a.n@lists.open-mesh.org Date: Tue, 20 Jul 2010 11:03:09 +0200 Message-Id: <1279616589-3086-1-git-send-email-sven.eckelmann@gmx.de> X-Mailer: git-send-email 1.7.1 X-Y-GMX-Trusted: 0 Subject: [B.A.T.M.A.N.] [PATCH] batman-adv: Calculate hamming weight using optimized kernel functions X-BeenThere: b.a.t.m.a.n@lists.open-mesh.org X-Mailman-Version: 2.1.11 Precedence: list Reply-To: The list for a Better Approach To Mobile Ad-hoc Networking List-Id: The list for a Better Approach To Mobile Ad-hoc Networking List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 20 Jul 2010 09:04:05 -0000 The Kernighan algorithm is not able to calculate the number of set bits in parallel and the compiler cannot replace it with optimized instructions. The kernel provides specialised functions for each cpu which can either use a software implementation or hardware instruction depending on the target cpu. Signed-off-by: Sven Eckelmann Reported-by: David S. Miller Signed-off-by: Sven Eckelmann --- batman-adv/bitarray.c | 15 +++++---------- batman-adv/bitarray.h | 3 ++- 2 files changed, 7 insertions(+), 11 deletions(-) diff --git a/batman-adv/bitarray.c b/batman-adv/bitarray.c index dd4193c..9dbaf1e 100644 --- a/batman-adv/bitarray.c +++ b/batman-adv/bitarray.c @@ -22,6 +22,8 @@ #include "main.h" #include "bitarray.h" +#include + /* returns true if the corresponding bit in the given seq_bits indicates true * and curr_seqno is within range of last_seqno */ uint8_t get_bit_status(TYPE_OF_WORD *seq_bits, uint32_t last_seqno, @@ -187,21 +189,14 @@ char bit_get_packet(TYPE_OF_WORD *seq_bits, int32_t seq_num_diff, } /* count the hamming weight, how many good packets did we receive? just count - * the 1's. The inner loop uses the Kernighan algorithm, see - * http://graphics.stanford.edu/~seander/bithacks.html#CountBitsSetKernighan + * the 1's. */ int bit_packet_count(TYPE_OF_WORD *seq_bits) { int i, hamming = 0; - TYPE_OF_WORD word; - for (i = 0; i < NUM_WORDS; i++) { - word = seq_bits[i]; + for (i = 0; i < NUM_WORDS; i++) + hamming += hweight_long(seq_bits[i]); - while (word) { - word &= word-1; - hamming++; - } - } return hamming; } diff --git a/batman-adv/bitarray.h b/batman-adv/bitarray.h index 01897d6..c0c1730 100644 --- a/batman-adv/bitarray.h +++ b/batman-adv/bitarray.h @@ -22,7 +22,8 @@ #ifndef _NET_BATMAN_ADV_BITARRAY_H_ #define _NET_BATMAN_ADV_BITARRAY_H_ -/* you should choose something big, if you don't want to waste cpu */ +/* you should choose something big, if you don't want to waste cpu + and keep the type in sync with bit_packet_count */ #define TYPE_OF_WORD unsigned long #define WORD_BIT_SIZE (sizeof(TYPE_OF_WORD) * 8)